Scheme case语句中缺少标识符的类型
我试图学习类型化的racket,但我遇到了一些类型注释问题Scheme case语句中缺少标识符的类型,scheme,racket,typed-racket,Scheme,Racket,Typed Racket,我试图学习类型化的racket,但我遇到了一些类型注释问题 #lang typed/racket (require typed/racket/gui) (define frame (new frame% [label "test frame"])) (define tab-panel (new tab-panel% [parent frame] [choices '("One" "Two" "Three")] [min-height 300]
#lang typed/racket
(require typed/racket/gui)
(define frame (new frame% [label "test frame"]))
(define tab-panel (new tab-panel% [parent frame] [choices '("One" "Two" "Three")]
[min-height 300] [min-width 300]
(callback
(lambda (tp e)
(case (send tp get-selection)
((0) (send tp change-children (lambda (children) (list a-panel))))
((1) (send tp change-children (lambda (children) (list b-panel))))
((2) (send tp change-children (lambda (children) (list c-panel)))))))))
(define a-panel (new panel% (parent tab-panel)))
(define a-text (new message% (parent a-panel) (label "A-panel")))
(define b-panel (new panel% (parent tab-panel)))
(define b-text (new message% (parent b-panel) (label "B-panel")))
(define c-panel (new panel% (parent tab-panel)))
(define c-text (new message% (parent c-panel) (label "C-panel")))
在case语句的行上生成以下错误:
. Type Checker: missing type for identifier;
consider adding a type annotation with `:'
identifier: a-panel in: a-panel
. Type Checker: missing type for identifier;
consider adding a type annotation with `:'
identifier: b-panel in: b-panel
. Type Checker: missing type for identifier;
consider adding a type annotation with `:'
identifier: c-panel in: c-panel
. Type Checker: Summary: 3 errors encountered in:
a-panel
b-panel
c-panel
我一直在翻阅文档,似乎无法找出类型声明的正确语法来解决这个问题
这不是家庭作业。我只是想学习打字技巧,因为我认为强打字是个好主意。您的
case
语句和GUI东西混淆了这个问题。有时,通过尝试较小的示例,可以更容易地找出问题所在。这里的问题与这个简单得多的程序中的问题相同:
#lang typed/racket
(define (list-abc)
(list a b c))
(define a 1)
(define b 2)
(define c 3)
它必须知道a
、b
和c
的类型,以便推断列表abc
的类型。有两种方法可以解决这个问题。在列表abc
上放置类型批注,或在a
、b
和c
上放置类型批注
要么:
(: list-abc : -> (Listof Integer))
(define (list-abc)
(list a b c))
或:
这个简单问题的解决方案也可以转化为更大的程序。您可以使用选项卡面板
上的类型注释,或者使用a-panel
、b-panel
和c-panel
上的类型注释来解决此问题
对于您的程序,注释将不是
(>(整型列表))
和整型
,而是(实例选项卡面板%)
和(实例面板%)
您的case
语句和GUI内容会混淆问题。有时,通过尝试较小的示例,可以更容易地找出问题所在。这里的问题与这个简单得多的程序中的问题相同:
#lang typed/racket
(define (list-abc)
(list a b c))
(define a 1)
(define b 2)
(define c 3)
它必须知道a
、b
和c
的类型,以便推断列表abc
的类型。有两种方法可以解决这个问题。在列表abc
上放置类型批注,或在a
、b
和c
上放置类型批注
要么:
(: list-abc : -> (Listof Integer))
(define (list-abc)
(list a b c))
或:
这个简单问题的解决方案也可以转化为更大的程序。您可以使用选项卡面板
上的类型注释,或者使用a-panel
、b-panel
和c-panel
上的类型注释来解决此问题
对于您的程序,注释将不是
(>(Listof Integer))
和Integer
,它们将是(实例选项卡面板%)
和(实例面板%)
当我尝试在面板的定义中添加注释时,我收到一个没有意义的不同错误:类型检查器:解析类型中的错误;类型名称“panel%”在中未绑定:panel%我知道它的意思,但发生错误毫无意义<代码>面板%在键入的/racket/guiok中定义。对象的类型是
(实例类类型)
,其中类类型
是类的类型。panel%
的类型是panel%
,因此其对象的类型是(实例面板%)
。这是在中描述的。就是这样!非常感谢。我做OO编程已经有几十年了,但我对打字的敲诈非常陌生。我真的很感谢你的帮助。我明白你为什么认为类型就是类名了。类型化的Racket类不直接用作类型,因为它们是一级值。相反,它们有(实例…
)来区分对象类型和类类型。当我尝试在面板的定义中添加注释时,我收到一个不同的错误,毫无意义:类型检查器:类型中的解析错误;类型名称“panel%”在中未绑定:panel%我知道它的意思,但发生错误毫无意义<代码>面板%在键入的/racket/guiok中定义。对象的类型是
(实例类类型)
,其中类类型
是类的类型。panel%
的类型是panel%
,因此其对象的类型是(实例面板%)
。这是在中描述的。就是这样!非常感谢。我做OO编程已经有几十年了,但我对打字的敲诈非常陌生。我真的很感谢你的帮助。我明白你为什么认为类型就是类名了。类型化的Racket类不直接用作类型,因为它们是一级值。相反,它们有(实例…
)来区分对象类型和类类型。