[racket] [typed] optional parameter

From: Eric Tanter (etanter at dcc.uchile.cl)
Date: Fri Nov 11 06:22:56 EST 2011

Hi,

A very simple question, but I could not find the answer in the guide/reference.

Suppose I have the identity over Numbers:

(: g (Number -> Number))
(define (g x)
  x)

Now I want to make x optional.

(: g (Number -> Number))
(define (g [x 10])
  x)

Type Checker: Expected Number, but got Any in: (define (g (x 0)) x)

I removed the type annotation to see what type is given to:
(define (g [x 10]) x)
it is:
(case-> (-> Positive-Byte) (Any -> Any))

but if I annotate g using case->, the same error happens:

(: g (case-> (-> Number) (Number -> Number)))
(define (g [x 10])
  x)

Type Checker: Expected Number, but got Any in: (define (g (x 10)) x)

Can someone explain why this error happens, and how to correctly annotate g?

Thanks,

-- Éric



Posted on the users mailing list.