JRR
JRR

Reputation: 6152

How can I specify a void function in typed racket?

What should be the type annotation of foo?

(define (foo)
  (println "hello"))

I tried these but none of them worked:

(: foo (-> () ()))
(: foo (-> Void Void))

Upvotes: 4

Views: 698

Answers (2)

Leif Andersen
Leif Andersen

Reputation: 22342

The type (-> Void Void) is for a function that takes in a void, and returns a void. Your foo function, takes in no arguments, and returns a void. As such, the type you want for it is actually:

(: foo (-> Void))
(define (foo)
  (println "hello"))

As a side note:

If you wanted to modify foo to have the type (-> Void Void), you could do this:

(define (foo _)
  (print "Don't do this though"))

As for:

(: foo (-> () ()))

That is syntactically invalid.

Upvotes: 5

soegaard
soegaard

Reputation: 31147

In this case Typed Racket can infer the type. Run this program:

#lang typed/racket
(define (foo)
  (println "hello"))

Then in the REPL you can write

> foo
- : (-> Void)
#<procedure:foo>

or

> (:print-type foo)
(-> Void)

to see that the type of foo is (-> Void). That is, it is a function of no argument that returns a value of type Void (that is, it returns #<void>.

Out final program becomes:

#lang typed/racket
(: foo : (-> Void))
(define (foo)
   (println "hello"))

Upvotes: 3

Related Questions