Re: get keys from defrecord

2011-08-30 Thread Ken Wesson
On Mon, Aug 29, 2011 at 10:14 PM, Steve Miner  wrote:
>
> On Aug 29, 2011, at 8:20 PM, Alex Miller wrote:
>
>> 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.

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.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: get keys from defrecord

2011-08-29 Thread Steve Miner

On Aug 29, 2011, at 8:20 PM, Alex Miller wrote:

> 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

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: get keys from defrecord

2011-08-29 Thread Lee Spector

On Aug 29, 2011, at 2:57 PM, Tassilo Horn wrote:
> 
> 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:

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

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: get keys from defrecord

2011-08-29 Thread Alex Miller
There is no good way to do this in 1.2 other than using the instance
mechanism that Aaron suggests.  I have had this need myself for
several meta-programming type use cases (building specialized record
serializers, universal record constructors, etc).  We wrap defrecord
in our own macros that generate multimethod implementations at record
construction time.

One possible solution is to use Java reflection to grab the fields
directly.  Clojure generates some in the class, but I think they all
start with an _, so you skip those and probably come up with the
correct fields.  However, I presume the names would be mangled and
you'd need to de-mangle them.

I'm not sure if there are any enhancements in the 1.3 record support
for this feature.



On Aug 29, 11:54 am, Razvan Rotaru  wrote:
> Hi,
>
> Assuming I have:
>
> (defrecord myrecord [:a :b :c])
>
> is there a way to get the list of keys from the record definition?
>
> Thanks,
> Razvan

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: get keys from defrecord

2011-08-29 Thread Laurent PETIT
Hi, it could help if you can provide more info on what you're trying to
achieve

2011/8/29 Razvan Rotaru 

> Hi,
>
> Assuming I have:
>
> (defrecord myrecord [:a :b :c])
>
> is there a way to get the list of keys from the record definition?
>
> Thanks,
> Razvan
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@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 post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: get keys from defrecord

2011-08-29 Thread Tassilo Horn
Aaron Bedra  writes:

> 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

> On 08/29/2011 12:54 PM, Razvan Rotaru wrote:
>> Hi,
>>
>> Assuming I have:
>>
>> (defrecord myrecord [:a :b :c])
>>
>> is there a way to get the list of keys from the record definition?
>>
>> Thanks,
>> Razvan
>>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: get keys from defrecord

2011-08-29 Thread Aaron Bedra

user=> (defrecord MyRecord [a b c])
user.MyRecord
user=> (def rec (user.MyRecord. "one" "two" "three"))
#'user/rec
user=> (keys rec)
(:a :b :c)


Cheers,

Aaron Bedra
--
Clojure/core
http://clojure.com

On 08/29/2011 12:54 PM, Razvan Rotaru wrote:

Hi,

Assuming I have:

(defrecord myrecord [:a :b :c])

is there a way to get the list of keys from the record definition?

Thanks,
Razvan



--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en