简体   繁体   English

你能在clojure defrecord中指定方法的返回类型吗?

[英]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 []))

You can type-hint the protocol ... 你可以输入提示协议......

(defprotocol ApplicationInfo
  (author ^String [obj])
  ; ...
  )

but I'm told that this type-hint is probably ignored (see this follow-up question). 但我被告知这种类型提示可能会被忽略(请参阅后续问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您可以对没有返回类型的void方法进行单元测试吗? - Can you unit test a void method which doesnt have return type? System.Void类型如何为不返回值的方法指定返回值类型? - How does System.Void type specify a return value type for a method that does not return a value? 如果存在没有方法的类型,您是否可以将其视为对象? - If there exist a type with no method can you consider it a object? 如果Java方法的返回类型是int,那么该方法是否可以返回byte类型的值? - If a Java method's return type is int, can the method return a value of type byte? 如何不指定任何返回类型,而以表格形式打印数组? - How can I not specify any return type and yet print my arrays in a tabular form? 两种返回类型的方法 - A Method with 2 Kind of Return Type 返回通用方法的类型 - Return type of generic method static方法不能返回struct类型?(C ++) - static method can't return struct type?(C++) 如何编写同时包含字符串和双精度类型的方法? 应该是void还是返回类型方法? - How can i write a method that contains both string and double type? should it be a void or return type method? 如何在不使用递归的情况下使用 int 返回类型方法返回 FIbonacci 序列? - How can I return the FIbonacci sequence using an int return type method without using recursion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM