clojure gen-class varargs constructor - clojure

in the :constructors map and subsequent -init definitions, how do I represent a varargs constructor (assuming the superclass has multiple constructors of which one is varargs) ?

Since varargs are essentially syntax sugar for Object arrays, you could just use "[Ljava.lang.Object;" as the type of constructor's parameter.
Here's some sample code:
(ns t.vtest
(:gen-class
:implements [clojure.lang.IDeref]
:init init
:state state
:constructors {["[Ljava.lang.Object;"] []}))
;; ^-----------------------
;; You should put "[Ljava.lang.Object;" for superclass varargs constructor here
;; I left it blank for the sake of working example
(defn -init
[args]
(println "first element of args" (aget args 0) "total elements" (alength args))
[[] (into [] args)])
(defn -deref
[this]
(.state this))
and that's how it looks in REPL
user=> #(t.vtest. (into-array Object ["A" "B" 1 2]))
first element of args A total elements 4
["A" "B" 1 2]

Since clojure don't support it at the moment you need to patch it with: https://groups.google.com/forum/#!topic/clojure/HMpMavh0WxA.
And use it with new meta tag:
(ns t.vtest
(:gen-class
:implements [clojure.lang.IDeref]
:init init
:state state
:constructors {^:varargs ["[Ljava.lang.Object;"] []}
))

Related

PersistentVector cannot be cast to class IAtom

I have a local variable that I must store some texts as a list/vector.
I want to add values to these vector according to some conditions, so what I wanna do is just add values to the vector and in the end have a updated version of this vector
But when I try to use the !swap I always get an error.
I've tried these two options:
(do
(let
[error-list ()]
(println (conj error-list "test"))
(swap! error-list conj "test2")))
The result:
(test)
Exception in thread "main" java.lang.ClassCastException: class clojure.lang.PersistentVector cannot be cast to class clojure.lang.IAtom (clojure.lang.PersistentVector and clojure.lang.IAtom are in unnamed module of loader 'app')
And:
(do
(let
[error-list []]
(println (conj error-list "test"))
(swap! error-list conj "test2")))
The result:
[test]
Exception in thread "main" java.lang.ClassCastException: class clojure.lang.PersistentList$EmptyList cannot be cast to class clojure.lang.IAtom (clojure.lang.PersistentList$EmptyList and clojure.lang.IAtom are in unnamed module of loader 'app')
That is not a local variable, that is a let binding. You have bound the name error-list to the value '() in the first snippet and [] in the second.
swap! is a function for updating the value of an atom. You do not have an atom, you have (first snippet) a List and (2nd snippet) a Vector. You can wrap either of these in an Atom to allow for mutable state using the atom function.
(do
(let
[error-list (atom [])]
(println (swap! error-list conj "test"))
(swap! error-list conj "test2")
#error-list))
[test]
["test" "test2"]
user=>

Clojure function self reference to get own metadata

When I attach some metadata to a function and then call it I am not able to access those metadata within that function
(let [I (fn I [x] (println I) (println (meta I)))]
(let [f (with-meta I {:rr 5})]
(println I)
(println f)
(f I)))
I see that the self reference from within the function is not the function instance actually invoked and thus no metadata is available through that self reference. I need the self reference to give me the function instance actually invoked to access those metadata
I think that the problem is that your conflating the value of the function and the identity of the function together. It's a thing many other languages do so it's natural when you're learning Clojure. In your example, I has a reference to itself, and looks up the metadata from that reference, which returns nil. You then create f which is the same as I, but with some metadata. So when you run f it looks up the metadata on I and returns nil. Defining f doesn't change I at all, it just creates a new thing in terms of the old thing. If you want to change something you need to introduce a reference type that you can change. There are several of these, but usually to store functions you'd use a Var (see here for reference)
(defn i [] (meta i))
(i) ;;=> nil
(alter-var-root #'i with-meta {:rr 5})
(i) ;;=> {:rr 5}
Here we define a function in the current namespace called i which just returns it's own metadata. We call it to get nil. Then we alter the global reference with some new metadata, and call it again.
If you wanted a more lexically scoped example, you could use an atom as below:
(let [i (atom nil)
f (fn [] (meta #i))]
(reset! i f)
(prn 'before '>> (#i))
(swap! i with-meta {:rr 5})
(prn 'after '>> (#i)))
However, other than learning how these things fit together, I'm not sure what the goal is. It's probably a bad idea to try and use these structures in a real program that you plan on maintaining.
Rather accidentally, I found a trick that enables functions to read it own metadata. It appears, the Clojure compiler generates metadata support code differently when the original function definition has custom metadata. If it is present, (meta fn-name) works inside the body of the function, otherwise it does not. For example, the following produces the result desired by the OP:
*clojure-version*
;;=> {:major 1, :minor 10, :incremental 0, :qualifier nil}
(let [f1 ^{:foo true} (fn f [] (meta f))
f2 (with-meta f1 {:bar true})]
(prn (f1))
(prn (f2)))
;;=> {:foo true}
;;=> {:bar true}
;;=> nil
We can examine the code generated for a function without the metadata in the original definition - there is just the invoke method
(require '[clojure.pprint :as p])
(let [ff (fn f [] (meta f))]
(p/pprint (seq (.getDeclaredMethods (class ff)))))
;;=> (#object[java.lang.reflect.Method 0x2b56b137 "public java.lang.Object user$eval2171$f__2172.invoke()"])
;;=> nil
And when the metadata is present, additional methods (meta and withMeta) are generated to deal with the metadata.
(let [ff ^:foo (fn f [] (meta f))]
(p/pprint (seq (.getDeclaredMethods (class ff)))))
;;=> (#object[java.lang.reflect.Method 0x3983bd83 "public clojure.lang.IObj user$eval2175$f__2176.withMeta(clojure.lang.IPersistentMap)"]
;;=> #object[java.lang.reflect.Method 0x547d182d "public clojure.lang.IPersistentMap user$eval2175$f__2176.meta()"]
;;=> #object[java.lang.reflect.Method 0x62c3d0fe "public java.lang.Object user$eval2175$f__2176.invoke()"])
;;=> nil
Welcome to Clojure, #xstreamer!
I'm going to suggest something different from what (precisely) you're asking for. I don't know how querying the function's metadata from within the function should work, really. So I'm going to suggest defining the function first, and redefining the function metadata afterwards. This is fairly simple in Clojure.
(defn f
"Boring doc"
[])
(meta #'f)
;; => {:arglists ([]),
;; :doc "Boring doc",
;; :line 32,
;; :column 1,
;; :file "C:/Users/teodorlu/IdeaProjects/th-scratch/src/th/play/core.clj",
;; :name f,
;; :ns #object[clojure.lang.Namespace 0x3b402f0c "th.play.core"]}
Now, redefine it!
(alter-meta! #'f assoc :rr 5)
(meta #'f)
;; => {:arglists ([]),
;; :doc "Boring doc",
;; :line 32,
;; :column 1,
;; :file "C:/Users/teodorlu/IdeaProjects/th-scratch/src/th/play/core.clj",
;; :name f,
;; :ns #object[clojure.lang.Namespace 0x3b402f0c "th.play.core"],
;; :rr 5}
Where assoc sets a value in a map.
(assoc {} :rr 5)
;; {:rr 5}
(assoc {:some :stuff} :more :stuff)
;; {:some :stuff, :more :stuff}
References
If you're confused by the #'f, this is how you get the var representing the binding of f, instead of just the value it refers to. For more information about vars and how to use them, refer to the official reference on vars and the less terse guide from 8th light.

clojure gen-class generated classes invocation issue

I defined the following MyCache.clj
(ns abcd.MyCache
(:gen-class
:name "abcd.MyCache"
:init "init"
:constructors { [java.text.DateFormat][] }
:methods [ [now [] void] [myformat [long] String] ]
:state "state"
:main false))
(defn -init[format]
([[] (atom {:format format})]))
(defn -now[this] ( (:format #(.state this)) (System/currentTimeMillis)))
(defn -myformat[this time]
( (:format #(.state this) (new java.util.Date time))))
I compiled the above file using (compile 'abcd.MyCache) successfully.
When I am trying to use the generated classes as shown below..I am getting errors. Please help.
user=> (new abcd.MyCache (new java.text.SimpleDateFormat "mmDDyyyy"))
IllegalArgumentException Key must be integer clojure.lang.APersistentVector.invoke (APersistentVector.java:265)
I don't feel well about this:
(defn -init[format]
([] [atom {:format format}]))
You are trying to get an element from a vector and it is expects an index (number).
What is correct is to deref the atom and get its value as the index of the vector. But again in your case, you are trying to query an empty vector.
Notice also, that [atom {:format format}] isn't the correct way to create an atom. You should use:
(atom {:format format})
And by the way, the following form is the preferred one to create Java objects (nothing wrong with (new) of course):
(Date.)
(DateFormat.)

Clojure multiple constructors using gen-class

How can i define multiple constructors and states using gen-class in clojure?
I do not see a way to do this with single valued mappings for :init, :state and :constructors.
Multiple Constructors
To have multiple constructors on the generated class you need all the constructor parameters specified in the :constructors option of gen-class, and the :init function should be multi-arity to match. Something like the following:
(ns something-amazing
(:gen-class :init myconstructor
:state state
:constructors {[String] []
[String String] []}))
(defn -myconstructor
([^String p1] [[] {:name p1 :special false}])
([^String p1 ^String p2] [[] {:name p1 :special p2}]))
In this case, both constructors would call the same zero-parameter super-type constructor, as specified by the empty vector values in the :constructor hash-map.
Multiple States
State is generally a hash-map, so you don't need multiple states. Just use keywords where you would use field names in an object.
{:name "name1"
:special false}
(defn -method1 [this] (:name (.state this)))

How to make a record from a sequence of values

I have a simple record definition, for example
(defrecord User [name email place])
What is the best way to make a record having it's values in a sequence
(def my-values ["John" "john#example.com" "Dreamland"])
I hoped for something like
(apply User. my-values)
but that won't work. I ended up doing:
(defn make-user [v]
(User. (nth v 0) (nth v 1) (nth v 2)))
But I'm sensing there is some better way for achieving this...
Warning: works only for literal sequables! (see MihaĊ‚'s comment)
Try this macro:
(defmacro instantiate [klass values]
`(new ~klass ~#values))
If you expand it with:
(macroexpand '(instantiate User ["John" "john#example.com" "Dreamland"]))
you'll get this:
(new User "John" "john#example.com" "Dreamland")
which is basically what you need.
And you can use it for instantiating other record types, or Java classes. Basically, this is just a class constructor that takes a one sequence of parameters instead of many parameters.
the defrecord function creates a compiled class with some immutable fields in it. it's not a proper clojure functions (ie: not a class that implements iFn). If you want to call it's constructor with apply (which expects an iFun) you need to wrap it in an anonymous function so apply will be able to digest it.
(apply #(User. %1 %2 %3 %4) my-values)
it's closer to what you started with though your approach of defining a constructor with a good descriptive name has its own charm :)
from the API:
Note that method bodies are
not closures, the local environment includes only the named fields,
and those fields can be accessed directy.
Writing your own constructor function is probably the way to go. As Arthur Ulfeldt said, you then have a function you can use as a function (e.g. with apply) rather than a Java-interop constructor call.
With your own constructor function you can also do argument validation or supply default arguments. You gain another level of abstraction to work with; you can define make-user to return a hash-map for quick development, and if you later decide to change to records, you can do so without breaking everything. You can write constructors with multiple arities, or that take keyword arguments, or do any number of other things.
(defn- default-user [name]
(str (.toLowerCase name) "#example.com"))
(defn make-user
([name] (make-user name nil nil))
([name place] (make-user name nil place))
([name user place]
(when-not name
(throw (Exception. "Required argument `name` missing/empty.")))
(let [user (or user (default-user name))]
(User. name user place))))
(defn make-user-keyword-args [& {:keys [name user place]}]
(make-user name user place))
(defn make-user-from-hashmap [args]
(apply make-user (map args [:name :user :place])))
user> (apply make-user ["John" "john#example.com" "Somewhere"])
#:user.User{:name "John", :email "john#example.com", :place "Somewhere"}
user> (make-user "John")
#:user.User{:name "John", :email "john#example.com", :place nil}
user> (make-user-keyword-args :place "Somewhere" :name "John")
#:user.User{:name "John", :email "john#example.com", :place "Somewhere"}
user> (make-user-from-hashmap {:user "foo"})
; Evaluation aborted.
; java.lang.Exception: Required argument `name` missing/empty.
One simple thing you can do is to make use of destructuring.
(defn make-user [[name email place]]
(User. name email place))
Then you can just call it like this
(make-user ["John" "John#example.com" "Dreamland"])
Update for Clojure 1.4
defrecord now defines ->User and map->User thus following in Goran's footstaps, one can now
(defmacro instantiate [rec args] `(apply ~(symbol (str "->" rec)) ~args))
which also works with non-literal sequences as in (instantiate User my-values).
Alternatively, along the lines of map->User one can define a function seq->User
(defmacro def-seq-> [rec] `(defn ~(symbol (str "seq->" rec)) [arg#] (apply ~(symbol (str "->" rec)) arg#)))
(def-seq-> User)
which will allow (seq->User my-values).
The idiomatic way to call a Record constructor is with the Clojure symbol ->MyRecord and that works just fine with apply.
(def my-values ["John" "john#example.com" "Dreamland"])
(defrecord User [name email place])
(apply ->User my-values)
; => #my-ns.User{:name "John",
:email "john#example.com",
:place "Dreamland"}