Can you specify the return type of a method in clojure defrecord?

I created an application and information interface and a class, but when I look at the generated classes, the return type for all methods is Object, can I change the return type to String? The documentation says that a hint type is possible with defrecord, but does not give an example, the only examples I could find were for hinting fields and method arguments.

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") ) 
+4
methods types clojure
source share
2 answers

See the definterface macro. Unlike defprotocol, the definterface macro provides a way to write the return type for methods.

Alan Malloy explains this pretty well here :

"Protocols are intended for consumption by Clojure functions that are not to be statically typed; interfaces are intended to be consumed by Java classes that must be statically typed."

Then you can use it, for example:

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

You can enter the hint protocol ...

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

but they tell me that this type hint is probably ignored (see this follow-up question).

+1
source share

All Articles