Re: difficulties using extend--'elp!

32 views
Skip to first unread message

Stephen Gilardi

unread,
Nov 24, 2015, 1:08:29 PM11/24/15
to clo...@googlegroups.com
There are a several small things here that need fixing:

- in the “gran” section of your “extend”, you’re defining “blap” which is not part of the gran protocol
  - perhaps blip was intended
- the first argument in protocol function declarations refers to the object itself, often named “this”
- calls to protocol functions don’t begin with “.”
- the call to (:blip this) had the wrong arity

Here is something similar that prints 6:

(ns aatree.record-play)

(defprotocol gran
  (blip [this x y z]))

(defrecord wackel [])

(defn new-wackel [opts]
  (let [w (->wackel)]
    (into w opts)
    (assoc w :blip (fn [this x y z] (+ x y z)))))

(extend wackel
  gran
  {:blip (fn [this x y z]
           ((:blip this) this x y z))})

(println (blip (new-wackel {}) 1 2 3))

- also consider extend-type in preference to extend here for its somewhat friendlier syntax.

—Steve

On Nov 24, 2015, at 11:30 AM, William la Forge <lafo...@gmail.com> wrote:

This is not working for me:

(ns aatree.record-play)

(defprotocol gran
(blip [x y z]))

(defrecord wackel [])

(defn new-wackel [opts]
(let [w (->wackel)]
(into w opts)
(assoc w :blip (fn [this x y z] (+ x y z)))))

(extend wackel
gran
{:blap (fn [this x y z]
((:blip this) x y z))})

(println (.blap (new-wackel {}) 1 2 3))

I get this: No matching method found: blap for class aatree.record_play.wackel

I changed the println to this:

(let [^gran w (new-wackel {})]
(println (.blap w 1 2 3)))

And now I get this: Unable to resolve classname: gran

Clearly there is something about protocols and/or extend that I do not understand.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

William la Forge

unread,
Nov 24, 2015, 1:19:36 PM11/24/15
to Clojure
Hi Steve!

I deleted this post, but still it gets mailed out.

Fixes are in and have been posted already.

See "Reduces Reuse Coupling".

But in any case, thanks. And thanks for the pointer to extend-type. :-)
Reply all
Reply to author
Forward
0 new messages