Вопрос

I have the following simple code:

; 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)))

How would I annotate the -main function using core.typed?

Это было полезно?

Решение

Because the -main function has more than one implementation, you need to explicitly use the function type, Fn, instead of the short syntax. It would look something like this:

(ann -main
     (Fn [-> nil]
         [Any -> nil]))

Take a look at the Functions entry, in the core.typed wiki, for more details on the syntax of the function type. Also, take a look at cf, because it can show you how to type forms.

(clojure.core.typed/cf
  (fn ([] (println "Hello, World!"))
      ([x] (println "You gave me " x))))

;; => [(Fn [Any -> nil] [-> nil]) {:then tt, :else ff}]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top