【问题标题】:How to annotate a function in Clojure using core.typed?如何使用 core.typed 在 Clojure 中注释函数?
【发布时间】:2013-10-09 00:44:41
【问题描述】:
我有以下简单的代码:
; No, test.core isn't the real namespace
(ns test.core
(:gen-class)
(:require [clojure.core.typed :refer [ann]]))
(defn -main
([]
(println "Hello, World!"))
([x]
(println "You gave me " x)))
如何使用core.typed 注释-main 函数?
【问题讨论】:
标签:
clojure
clojure-core.typed
【解决方案1】:
因为-main 函数有多个实现,您需要显式使用函数类型Fn,而不是短语法。它看起来像这样:
(ann -main
(Fn [-> nil]
[Any -> nil]))
查看 core.typed wiki 中的 Functions 条目,了解有关函数类型语法的更多详细信息。另外,看看cf,因为它可以告诉你如何输入表格。
(clojure.core.typed/cf
(fn ([] (println "Hello, World!"))
([x] (println "You gave me " x))))
;; => [(Fn [Any -> nil] [-> nil]) {:then tt, :else ff}]