Do you know of a reason why (deftype [foo & more]) isn't read in as
having two fields where the second is a seq?

Barring that, would it be reasonable to disallow & as a valid field
name, thus preventing this class of error?


On Nov 4, 11:54 pm, Christophe Grand <christo...@cgrand.net> wrote:
> 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