gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:44:06 25 4
gpt4 key购买 nike

我有以下简单代码:

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

最佳答案

因为-main函数有不止一种实现,所以需要显式使用函数类型Fn,而不是短语法。它看起来像这样:

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

看看 Functions core.typed wiki 中的条目,以获取有关函数类型语法的更多详细信息。另外,看看 cf ,因为它可以向您展示如何键入表单。

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

;; => [(Fn [Any -> nil] [-> nil]) {:then tt, :else ff}]

关于clojure - 如何使用 core.typed 在 Clojure 中注释函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19240000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com