gpt4 book ai didi

methods - 您可以在 clojure defrecord 中指定方法的返回类型吗?

转载 作者:行者123 更新时间:2023-12-02 14:40:52 25 4
gpt4 key购买 nike

我已经创建了一个应用程序信息接口(interface)和一个类,但是当我查看生成的类时,所有方法的返回类型都是 Object,我可以将返回类型更改为 String 吗?文档说类型提示可以使用 defrecord 但没有给出示例,我能找到的唯一示例是类型提示字段和方法参数。

src/com/vnetpublishing.clj

(ns com.vnetpublishing)

(defprotocol ApplicationInfo
(author [obj])
(author-email [obj])
(copyright [obj])
(app-name [obj])
(version [obj])
)

src/Physics.clj

(ns Physics)

(defrecord info [] com.vnetpublishing.ApplicationInfo
(author [this] "Ralph Ritoch")
(author-email [this] "Ralph Ritoch <root@localhost>")
(copyright [this] "Copyright \u00A9 2014 Ralph Ritoch. All rights reserved.")
(app-name [this] "Physics")
(version [this] "0.0.1-alpha")
)

最佳答案

看看definterface宏。与 defprotocol 不同,definterface 宏提供了一种为方法编写返回类型提示的方法。

艾伦·马洛伊对此解释得很好 here :

“协议(protocol)供 Clojure 函数使用,但不是应该是静态类型的;接口(interface)供消费Java 类,需要静态类型。”

然后您可以像这样使用它:

(definterface Test
(^void returnsVoid [])
(^int returnsInt [])
(^long returnsLong [])
(^String returnsString [])
(^java.util.HashMap returnsJavaUtilHashMap []))

关于methods - 您可以在 clojure defrecord 中指定方法的返回类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22954681/

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