FAQ
I've just started learning protocols, deftype, etc. The first thing I
did was try to extend a Clojure type (maps) to operate as a
specialized Java Swing interface (AttributeSet), forgetting that
interfaces are not protocols; i.e.

(extend-type clojure.lang.PersistentArrayMap
javax.swing.text.AttributeSet
(getAttribute [this k]
(get this k)))

Is there any way of doing what is intended here: get Clojure maps to
implement Java's AttributeSet? I've seen Chas Emerick's flowchart,
but I'm still trying to wrap my head around all the choices.

Thanks, Andrew

--
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Search Discussions

  • Stuart Sierra at Feb 10, 2012 at 2:24 pm
    Nope, can't be done. Java interfaces can't do this.

    Java 8 may have "Interface Injection" which will make this possible.
    -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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Drewn at Feb 10, 2012 at 5:51 pm
    Nope, can't be done. Java interfaces can't do this.
    I'm glad I asked the question. Given that it can't be done, any
    suggestions for the best way of handling this interop scenario?

    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Aaron Cohen at Feb 10, 2012 at 6:07 pm

    On Fri, Feb 10, 2012 at 3:13 AM, drewn wrote:
    I've just started learning protocols, deftype, etc.  The first thing I
    did was try to extend a Clojure type (maps) to operate as a
    specialized Java Swing interface (AttributeSet), forgetting that
    interfaces are not protocols; i.e.

    (extend-type clojure.lang.PersistentArrayMap
    javax.swing.text.AttributeSet
    (getAttribute [this k]
    (get this k)))
    I think in this case you should be able to use proxy, maybe.

    user=> (defn attribute-list [map] (proxy [javax.swing.text.AttributeSet] []
    (getAttribute [k] (get map k))))

    user=> (.getAttribute (attribute-list {:a 1 :b 2}) :a)
    1

    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Cedric Greevey at Feb 10, 2012 at 6:32 pm

    On Fri, Feb 10, 2012 at 1:07 PM, Aaron Cohen wrote:
    On Fri, Feb 10, 2012 at 3:13 AM, drewn wrote:
    I've just started learning protocols, deftype, etc.  The first thing I
    did was try to extend a Clojure type (maps) to operate as a
    specialized Java Swing interface (AttributeSet), forgetting that
    interfaces are not protocols; i.e.

    (extend-type clojure.lang.PersistentArrayMap
    javax.swing.text.AttributeSet
    (getAttribute [this k]
    (get this k)))
    I think in this case you should be able to use proxy, maybe.

    user=> (defn attribute-list [map] (proxy [javax.swing.text.AttributeSet] []
    (getAttribute [k] (get map k))))

    user=> (.getAttribute (attribute-list {:a 1 :b 2}) :a)
    1
    There's also defrecord:

    (defrecord foo [my basis keys]
    the.JavaInterface
    (javaIfaceMethod [this foo] ...))

    which will work in the specific case that you want "a Clojure map that
    implements a particular Java interface". Records behave partially as
    Clojure maps. For more general mixins, proxy, reify, and deftype are
    your friends, and even gen-class may be needed in some instances.

    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en
  • Drewn at Feb 10, 2012 at 8:36 pm
    There's also defrecord
    I considered using that, but I need to do something more with the
    constructor (e.g. convert the map into a Java array for internal
    use). Also, defrecords only takes positional arguments, which will be
    hard to use with tens of arguments. (An alternative is just to pass
    in one argument as a map itself, but that seems to defeat the
    purpose.)
    On Feb 10, 10:32 am, Cedric Greevey wrote:
    On Fri, Feb 10, 2012 at 1:07 PM, Aaron Cohen wrote:
    On Fri, Feb 10, 2012 at 3:13 AM, drewn wrote:
    I've just started learning protocols, deftype, etc.  The first thing I
    did was try to extend a Clojure type (maps) to operate as a
    specialized Java Swing interface (AttributeSet), forgetting that
    interfaces are not protocols; i.e.
    (extend-type clojure.lang.PersistentArrayMap
    javax.swing.text.AttributeSet
    (getAttribute [this k]
    (get this k)))
    I think in this case you should be able to use proxy, maybe.
    user=> (defn attribute-list [map] (proxy [javax.swing.text.AttributeSet] []
    (getAttribute [k] (get map k))))
    user=> (.getAttribute (attribute-list {:a 1 :b 2}) :a)
    1
    There's also defrecord:

    (defrecord foo [my basis keys]
    the.JavaInterface
    (javaIfaceMethod [this foo] ...))

    which will work in the specific case that you want "a Clojure map that
    implements a particular Java interface". Records behave partially as
    Clojure maps. For more general mixins, proxy, reify, and deftype are
    your friends, and even gen-class may be needed in some instances.
    --
    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+unsubscribe@googlegroups.com
    For more options, visit this group at
    http://groups.google.com/group/clojure?hl=en

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupclojure @
categoriesclojure
postedFeb 10, '12 at 8:13a
activeFeb 10, '12 at 8:36p
posts6
users4
websiteclojure.org
irc#clojure

People

Translate

site design / logo © 2022 Grokbase