5 ms·
I find it odd that he is using the new macro to create the record instances. I thought that it is meant to be used only in Java inter-op, as it requires that th
by rapala 14y ago
I find it odd that he is using the new macro to create the record instances. I thought that it is meant to be used only in Java inter-op, as it requires that the class of the record is visible in the current namespace.
- stevelosh 14y agoI don't get what you mean. I like the way new looks as compared to the Record. syntax, but either way you need the java class visible in the current namespace: sjl at grendel in ~/s/caves ><((°> lein2 repl caves.core=> (use 'caves.world) nil caves.core=> (new World [:foo]) CompilerException java.lang.IllegalArgumentException: Unable to resolve classname: World, compiling:(NO_SOURCE_PATH:1) caves.core=> (import '[caves.world World]) caves.world.World caves.core=> (new World [:foo]) #caves.world.World{:tiles [:foo]} caves.core=> Bye for now! sjl at grendel in ~/s/caves ><((°> lein2 repl caves.core=> (use 'caves.world) nil caves.core=> (World. [:foo]) CompilerException java.lang.IllegalArgumentException: Unable to resolve classname: World, compiling:(NO_SOURCE_PATH:1) caves.core=> Is there another way to create record instances that doesn't involve that warty (import) call that I'm not aware of?
- bitsai 14y agoAs of Clojure 1.3, defrecord (and deftype) automatically define factory functions in the same namespace as the record itself. defrecord generates 2 such functions, one that takes positional arguments and one that takes a map. So once you have :use'd or :require'd the namespace, you can do stuff like the following without having to import anymore: (->World [:foo]) (->World {:tiles [:foo]}) More details here: http://dev.clojure.org/display/design/defrecord+improvements http://dev.clojure.org/display/design/defrecord+improvements
- stevelosh 14y agoOh cool, I didn't know that. I kind of blocked 1.3 out of my memory after the "let's remove clojure.contrib and break everyone's shit" mess. It's good to know that progress toward patching all the places Java tends to leak through into Clojure is happening. I can't say I'm a fan of the punctuation-laden syntax, but I'll pick my battles and not complain about it. I'll switch over to this new form tonight and mention it in one of the later posts. Thanks!
- rapala 14y agoThis, and before 1.3 you were supposed to define a constructor function yourself. I'm pretty sure that was somewhere in the documentation. Or in the clojure.org/datatypes. Or in some mailing list post that I red. The Clojure documentation is a mess.