如何使用core.typed在Clojure中注释函数?

时间:2013-10-08 05:47:10

标签: clojure clojure-core.typed

我有以下简单的代码:

; 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函数?

1 个答案:

答案 0 :(得分:4)

因为-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}]