0
votes

I'm trying to learn typed racket and I'm running into some type annotation issues.

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

Generates the following errors on the lines in the case statement:

. 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

I've been digging through the documentation and I cannot seem to figure out the correct syntax for the type declaration to fix this issue.

This is not homework. I'm just trying to learn typed racket because I think strong typing is a good idea.

1

1 Answers

2
votes

Your case statement and GUI stuff obfuscate the problem. Sometimes it can be easier to figure out what's wrong by trying out smaller examples. Here the problem is the same as the problem in this much simpler program:

#lang typed/racket

(define (list-abc)
  (list a b c))

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

It has to know the types of a, b, and c in order to infer the type of list-abc. There are two ways to solve this. Either put a type annotation on list-abc, or put type annotations on a, b, and c.

Either:

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

Or:

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

This solution to the simpler problem also translates to your larger program. You can solve it either with a type annotation on tab-panel, or with type annotations on a-panel, b-panel, and c-panel.

For your program, the annotations wouldn't be (-> (Listof Integer)) and Integer, they would be (Instance Tab-Panel%) and (Instance Panel%).