Create an instance of a record using a string to define the record's symbol

49 views
Skip to first unread message

Colin Yates

unread,
Aug 6, 2014, 6:37:32 AM8/6/14
to clo...@googlegroups.com
Hi,

tldr; if I have a (defrecord MyRecord) in ns a.b.c and I have a string "a.b.c.MyRecord" how I can invoke (a.b.c.MyRecord.) (or (new a.b.c.MyRecord)?

I thought this was going to be as simple as (defn cfn "a.b.c.MyRecord") ((symbol cfn).) but that throws an ArityException: wrong number of args (0) passed to Symbol.  I 

I have a protocol with a number of implementations.  Each installation will have a single implementation and will chose one based on an environment variable.  At runtime I need to look at that environment variable which defines the fully qualified implementation of a protocol.  I then need to instantiate that record.

Architecturally equivalent to a simple plugin system.  I could alias them all but that requires knowing about all the implementations which is a no-go.

This is where I feel the pain of not having OSGi :).

Thanks all!

Ambrose Bonnaire-Sergeant

unread,
Aug 6, 2014, 6:51:11 AM8/6/14
to clojure
Hi Colin,

If you must call the Java constructor, you need reflection.

user=> (defrecord B [c])
user.B
user=> (def s "user.B")
#'user/s
user=> (.newInstance (first (.getDeclaredConstructors (Class/forName s))) (object-array [1]))
#user.B{:c 1}

Thanks,
Ambrose


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Yates

unread,
Aug 6, 2014, 6:56:00 AM8/6/14
to clo...@googlegroups.com
Ah OK, I didn't realise I needed to get into Java interop.

For education - can you (or anyone) explain me as to why what I was trying didn't work?

Thanks Ambrose.


You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/fp7I-zAuu2c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Jozef Wagner

unread,
Aug 6, 2014, 6:57:32 AM8/6/14
to clo...@googlegroups.com, abonnair...@gmail.com
You can use record's positional constructor:

user> (defrecord R [a])
user.R
user> ((find-var (symbol "user/->R")) 5)
#user.R{:a 5}

JW

Ambrose Bonnaire-Sergeant

unread,
Aug 6, 2014, 7:02:59 AM8/6/14
to clojure
The `new` reader macro must resolve its first argument at compile time.

That's why (new (symbol s)) doesn't work.

Thanks,
Ambrose

Colin Yates

unread,
Aug 6, 2014, 7:46:12 AM8/6/14
to clo...@googlegroups.com, abonnair...@gmail.com
Thanks both.
Reply all
Reply to author
Forward
0 new messages