如何在类型球拍中指定 void 函数?

How can I specify a void function in typed racket?

foo 的类型注释应该是什么?

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

我尝试了这些,但 none 成功了:

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

在这种情况下,Typed Racket 可以推断类型。 运行 这个程序:

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

然后在REPL中你可以这样写

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

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

看到foo的类型是(-> Void)。 也就是说,returns 类型为 Void 的值是一个无参函数(即 returns #<void>.

输出最终程序变为:

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

类型 (-> Void Void) 用于接受 void 和 returns 的函数 void。您的 foo 函数不接受任何参数,并且 returns 一个 void。因此,您想要的类型实际上是:

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

旁注:

如果您想将 foo 修改为 (-> Void Void) 类型,您可以这样做:

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

至于:

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

这在语法上是无效的。