On Fri, Nov 5, 2010 at 1:39 AM, Mike Meyer <
mwm-keyword-googlegroups.620...@mired.org> wrote:

> It seems like the polymorphism of protocols breaks inside the
> methods. This is a problem for having a function that's polymorphic
> between an object and a container of the same objects.
>
> For instance:
>
> user=> (defprotocol Tune (tweek [this]))
> Tune
> user=> (deftype Knob [name] Tune (tweek [this] (println "Tweeked" name)))
> user.Knob
> user=> (def base (Knob. "base"))
> #'user/base
> user=> (tweek base)
> Tweeked base
> nil
> user=> (def treble (Knob. "treble"))
> #'user/treble
> user=> (tweek treble)
> Tweeked treble
> nil
> user=> (deftype Box [& knobs] Tune (tweek [this] (for [knob knobs] (tweek
> knob))))
>

this vector is not an argument list but a field list, so you are defiing to
fields, one named & and one named knobs.
Try to evaluate (.name (.knobs (Box. base treble))) and you'll see that
knobs only holds treble.

if you fix it:
(deftype Box [knobs] Tune (tweek [this] (for [knob knobs] (tweek knob))))

then:
(tweek (Box. [base treble]))

will work as expected.

If you really want var-args, you just have to write a factory fn
(defn box [& knobs] (Box. knobs))

hth,

Christophe

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

Reply via email to