Cheers,
Aaron Bedra
--
Clojure/core
http://clojure.com
> user=> (defrecord MyRecord [a b c])
> user.MyRecord
> user=> (def rec (user.MyRecord. "one" "two" "three"))
> #'user/rec
> user=> (keys rec)
> (:a :b :c)
I guess the problem with that is that you need to have an instance of
the record before you can use `keys'. And to create an instance, at
least you have to know the number of keys.
However, you can inspect the record's constructor using reflection:
(-> MyRecord .getConstructors first .getParameterTypes)
This gets you an array of the record's parameter types. It seems, that
the last 2 Object parameters are some internal stuff (metadata?), so the
array length - 2 is the arity.
(-> MyRecord .getConstructors first .getParameterTypes)
==> 5 ;; so the record has 3 keys
If the record doesn't have fields hinted with primitive types, then
that's enough knowledge to create an instance. If there are primitive
type hints, then the constructor has primitive type parameters you have
to match when calling it.
All in all, it seems there's everything you need to create a function
that creates a dummy instance of arbitrary (unknown) records that you
can query for its keys afterwards.
But maybe there's a better way... :-)
Bye,
Tassilo
--
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
Or, if structs will do for your application, then you can do this more simply:
user=> (defstruct mystruct :a :b :c)
#'user/mystruct
user=> (keys (struct-map mystruct))
(:a :b :c)
-Lee
> I'm not sure if there are any enhancements in the 1.3 record support
> for this feature.
In 1.3beta2, the record class has a static method getBasis that will give you the fields. I remember Fogus mentioning this on the mailing list. The design notes [1] say "These methods should not be used in Clojure code" as they're intended for tool support, but they seem generally useful to me.
user=> (clojure-version)
"1.3.0-beta2"
user=> (defrecord MyRecord [a b])
user.MyRecord
user=> (MyRecord/getBasis)
[a b]
[1] http://dev.clojure.org/display/design/defrecord+improvements
Steve Miner
In a Lisp, anything useful for "tool support" and accessible from
inside the language tends to be useful sometimes in macros as well;
perhaps macros need to be considered part of "tool support" for a
Lisp. After all, they are used to extend and metaprogram the language.
--
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.