我的point
记录类型定义如下:
(define-record-type point
(make-point x y)
point?
(x point-x)
(y point-y)
)
现在,我想扩展point
记录类型并定义一个新的记录类型,如下所示:
(define-record-type cpoint
(make-cpoint color)
cpoint?
(color cpoint-color)
(parent point)
)
当我运行上面的方案shell中的定义一切正常。我可以正确构建point
类型。但是,当我尝试按如下方式构造cpoint
类型时:
(define p2 (make-cpoint 8 9 'red))
我收到以下错误:
... rfi / 9 / record.rkt:100:28:arity mismatch ;;预期的数量 参数与给定的数字不匹配;预期:1;给出:3; [,bt for context]
我认为cpoint
是point
的孩子,它应该在其构造函数中接受point
类型的参数。
我该如何使这项工作?
P.S我是Scheme的新手。
答案 0 :(得分:1)
SRFI-9中没有儿童记录。因此,您需要单独指定它们:
@PostMapping(value="/todaydatarecover.json")
@ResponseBody
public ModelAndView todayInfoAndIdRecover(@RequestBody TodayData todayData, ModelAndView model, HttpServletRequest request) throws IOException
{
因此,(define-record-type cpoint
(make-cpoint x y color)
cpoint?
(x cpoint-x)
(y cpoint-y)
(color cpoint-color))
和x
获取y
和cpoint
的访问者不同。
在R6RS中,您有(rnrs records syntactic (6))
,类似于SRFI-9,但不兼容。您的代码如下:
point
您已标记了Racket,如果您使用的是默认语言#!r6rs
(import (rnrs base)
(rnrs records syntactic))
(define-record-type (point make-point point?)
(fields (immutable x point-x)
(immutable y point-y)))
(define-record-type (cpoint make-cpoint cpoint?)
(fields (immutable c cpoint-c))
(parent point))
(make-cpoint 4 5 'red) ; ==> implementation chosen visual representation, perhaps #cpoint-4-5-red
,则他们有#lang racket
:
struct
我添加了#lang racket
(struct point (x y) #:transparent)
(struct cpoint point (color) #:transparent)
(cpoint 4 5 'red) ; ==> (cpoint 4 5 'red)
,因为它是R6RS中的默认值。你真的希望构造函数名称为#:transparent
,你需要指定它:
make-xxx