Re: overriding keyword behavior?

2011-03-11 Thread Joost
On Mar 10, 5:31 pm, kurtharriger  wrote:
> That was basically my question, protocols are designed for this but
> ILookup isn't a protocol.  Is it possible to make ILookup into a
> protocol?  I haven't looked at the java code much at all, but I might
> take a stab at creating a patch it that seems possible.
>
> Converting java.util.Properties into a map isn't hard, but converting
> java objects to native maps is extreamly common (bean, reflect, ring,
> etc...) and perhaps unnecessary in many situations if one could just
> extend an existing java type with ILookup so that if it looks like a
> duck, its a duck.

I've had the same idea a couple of day ago; specifically, looking up
public members of an object.

In the end, I think the actual use cases generally are more involved
than just :keyword lookup, so I wrote some code to automatically do
the conversion to map:

https://github.com/joodie/clj-java-fields

-- 
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: overriding keyword behavior?

2011-03-11 Thread Stuart Sierra
It is not possible to turn an interface into a protocol. When protocols 
first appeared, the idea was floated to replace the low-level interfaces 
like ILookup with equivalent protocols, but this idea has not received 
substantial attention since.

-Stuart Sierra
clojure.com

-- 
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: overriding keyword behavior?

2011-03-10 Thread kurtharriger
That was basically my question, protocols are designed for this but
ILookup isn't a protocol.  Is it possible to make ILookup into a
protocol?  I haven't looked at the java code much at all, but I might
take a stab at creating a patch it that seems possible.

Converting java.util.Properties into a map isn't hard, but converting
java objects to native maps is extreamly common (bean, reflect, ring,
etc...) and perhaps unnecessary in many situations if one could just
extend an existing java type with ILookup so that if it looks like a
duck, its a duck.

- Kurt



On Mar 8, 3:34 pm, Stuart Sierra  wrote:
> Oh yeah, you can't extend an interface to a new class.  So it won't work.
>  This is why protocols exist, in fact.  Silly me.
>
> -S

-- 
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: overriding keyword behavior?

2011-03-08 Thread Stuart Sierra
Oh yeah, you can't extend an interface to a new class.  So it won't work. 
 This is why protocols exist, in fact.  Silly me.

-S

-- 
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: overriding keyword behavior?

2011-03-07 Thread Ken Wesson
On Mon, Mar 7, 2011 at 9:34 PM, Stuart Sierra
 wrote:
> Yes, something like tonyl's code will work.

OH RLY?

#

-- 
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: overriding keyword behavior?

2011-03-07 Thread Stuart Sierra
Yes, something like tonyl's code will work.

-S

-- 
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: overriding keyword behavior?

2011-03-07 Thread tonyl
I little redundant but looking now at the source code in Keyword.java

the Keyword class extends IFn, Comparable, Named, and Serializable;
they are all java that you could extend.
But I am not sure how the keyword lookup system works, haven't look
into it.
Just something that might help.

On Mar 6, 9:27 pm, kurtharriger  wrote:
> I was curious if it was possible to extend keyword lookup behavior to
> additional types, such as java.util.Properties.
>
> Currently I just convert java.util.properties into a keyword map with
> something such as the following.
> (defn as-keyword-map [prop]
>   (into {} (for [[k v] prop] [(keyword k) v])))
>
> This works well enough but I thought this might be possible to extend
> keywords or java.util.Properties via protocols or something to change
> the implementation of (:access-key prop) to (.get "access-key" prop).
> Its easy enough to redefine get using multimethods so that (get
> prop :access-key) would work on any type I wanted but I couldn't
> figure out how to do the same with keywords.  Looking at keyword.java
> it expects that object implements ILookup so I tried using (extend-
> type java.util.Properties Lookup ...) however ILookup isn't a protocol
> so this doesn't seem to work.
>
> I'm beginning to think that implementation of keywords being
> implemented in java doesn't support clojures more advanced
> extensibility features.  This makes me wonder if using the more
> idiomatic (:access-key obj) instead of (get obj :access-key) also
> limits my future extensibility options.  Is there a way to do this
> that I haven't thought of? and would it be possible to make ILookup
> into a protocol to enable this type of extensibility?

-- 
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: overriding keyword behavior?

2011-03-07 Thread tonyl
I haven't tested it or anything, but you can use:
(extend-type java.util.Properties
ILookup
(valAt [this key] ...)
(valAt [this key not-found] ...))

AFAIK you can extend on Interfaces but not Abstracts or Classes.

On Mar 6, 9:27 pm, kurtharriger  wrote:
> I was curious if it was possible to extend keyword lookup behavior to
> additional types, such as java.util.Properties.
>
> Currently I just convert java.util.properties into a keyword map with
> something such as the following.
> (defn as-keyword-map [prop]
>   (into {} (for [[k v] prop] [(keyword k) v])))
>
> This works well enough but I thought this might be possible to extend
> keywords or java.util.Properties via protocols or something to change
> the implementation of (:access-key prop) to (.get "access-key" prop).
> Its easy enough to redefine get using multimethods so that (get
> prop :access-key) would work on any type I wanted but I couldn't
> figure out how to do the same with keywords.  Looking at keyword.java
> it expects that object implements ILookup so I tried using (extend-
> type java.util.Properties Lookup ...) however ILookup isn't a protocol
> so this doesn't seem to work.
>
> I'm beginning to think that implementation of keywords being
> implemented in java doesn't support clojures more advanced
> extensibility features.  This makes me wonder if using the more
> idiomatic (:access-key obj) instead of (get obj :access-key) also
> limits my future extensibility options.  Is there a way to do this
> that I haven't thought of? and would it be possible to make ILookup
> into a protocol to enable this type of extensibility?

-- 
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