case 语句中缺少标识符类型

Missing type for identifier in case statement

我正在尝试学习打字球拍,但我 运行 遇到了一些类型注释问题。

#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)

它必须知道 abc 的类型才能推断出 list-abc 的类型。有两种方法可以解决这个问题。要么在 list-abc 上放置类型注释,要么在 abc.

上放置类型注释

或者:

(: list-abc : -> (Listof Integer))
(define (list-abc)
  (list a b c))

或者:

(define a : Integer 1)
(define b : Integer 2)
(define c : Integer 3)

这种针对更简单问题的解决方案也适用于您的大型程序。您可以使用 tab-panel 上的类型注释或 a-panelb-panelc-panel.

上的类型注释来解决它

对于您的程序,注释不会是 (-> (Listof Integer))Integer,而是 (Instance Tab-Panel%)(Instance Panel%)