TG-T
TG-T

Reputation: 2174

clojurescript: Trouble extending IAssociative assoc method

Ok, so I have two versions of a function here. One works, one doesn't. Code is based on the domina source; I'm expanding the same concept to google maps. Working one:

(defn- create-listener-function
[f type]
(fn [evt]
  (f (reify
      ILookup
      (-lookup [o k]
         (if-let [val (aget evt k)]
           val
           (aget evt (name k))))
      (-lookup [o k not-found] (or (-lookup o k)
                                not-found))
      IAssociative
      (-assoc  [o k v]
         (aset o (name k) v))))
true))

Ok, so the above works fine, and I can lookup members of the returned object like so:

 (:latLng obj)

However, when I try to assoc something with the returned object, using the below code, I can no longer retrieve the object's properties.

  [f type]
  (fn [evt]
  (f (assoc (reify
             ILookup
             (-lookup [o k]
               (if-let [val (aget evt k)]
                 val
                 (aget evt (name k))))
             (-lookup [o k not-found] (or (-lookup o k)
                                         not-found))
             IAssociative
             (-assoc  [o k v]
               (aset o (name k) v)))
       :type type))
 true))

What am I missing?

Upvotes: 1

Views: 887

Answers (1)

jbm
jbm

Reputation: 2600

I think you may just need to refer to the IAssociative protocol by its fully qualified name, cljs.core.IAssociative. In any case, the simplified example below works for me if I use the qualified name but not with the shorter name.

However, I found the same thing for ILookup, (I needed to use cljs.core.ILookup), so if that's working for you without the full name then I'm perhaps there's just something different about my or your setup.

cljs.user> (let [a (array 0 1 2 3)]
             (def obj
               (reify
                 cljs.core.IAssociative
                 (-assoc [_ k v]
                   (aset a k v)
                   (vec a)))))
nil
cljs.user> (assoc obj 0 'zero)
[zero 1 2 3]
cljs.user> (assoc obj 1 'one)
[zero one 2 3]

Upvotes: 1

Related Questions