[英]Can you specify the return type of a method in a clojure defrecord?
I've created an application-info interface and a class but when I review the generated classes the return type for all of the methods is Object, can I change the return type to String? 我已经创建了一个application-info接口和一个类但是当我查看生成的类时,所有方法的返回类型都是Object,我可以将返回类型更改为String吗? The documentation says type hinting is possible with defrecord but doesn't give an example, the only examples I could find were for type hinting fields and method arguments. 文档说使用defrecord可以进行类型提示但不提供示例,我能找到的唯一示例是类型提示字段和方法参数。
src/com/vnetpublishing.clj SRC / COM / vnetpublishing.clj
(ns com.vnetpublishing)
(defprotocol ApplicationInfo
(author [obj])
(author-email [obj])
(copyright [obj])
(app-name [obj])
(version [obj])
)
src/Physics.clj 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")
)
Look at definterface macro. 看看definterface宏。 Unlike defprotocol, the definterface macro provide a way to write return type hint for methods. 与defprotocol不同,definterface宏提供了一种为方法编写返回类型提示的方法。
Alan Malloy explain this pretty well here : Alan Malloy 在这里解释得非常好:
"Protocols are for consumption by Clojure functions, which aren't supposed to be statically typed; interfaces are for consumption by Java classes, which are required to be statically typed." “协议是由Clojure函数使用的,它们不应该是静态类型的;接口是供Java类消费的,需要静态类型化。”
You can then use it like: 然后你可以使用它:
(definterface Test
(^void returnsVoid [])
(^int returnsInt [])
(^long returnsLong [])
(^String returnsString [])
(^java.util.HashMap returnsJavaUtilHashMap []))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.