Fork me on GitHub
#clojurescript
<
2019-11-29
>
borkdude23:11:16

I'm having problems getting this to work in CLJS. It works in CLJ though:

(defprotocol IBox
  (setVal [_ _])
  (getVal [_]))

(deftype Foo [^:mutable v]
  IBox
  (setVal [this x]
    (set! (.-v this) x))
  (getVal [this] v))

(defn test-type []
  (let [x (Foo. 10)]
    (setVal x 11) ;; No protocol method IBox.setVal defined for type number: 11
    (println (getVal x))))

borkdude23:11:28

what's going on here?

borkdude23:11:17

it's because of the underscores?

(defprotocol IBox
  (setVal [_this _val])
  (getVal [_this]))
does seem to work 😕

borkdude23:11:30

seems like a bug to me

andy.fingerhut23:11:47

I am not sure whether ^:mutable has any effect in ClojureScript, but that probably isn't the root cause of whatever problems you are seeing.

borkdude23:11:39

no, it was the underscores: (setVal [_ _])

borkdude23:11:37

so it gets confused with two fields with the same underscore name

andy.fingerhut23:11:37

Maybe it gets confused with two args that have the same name, regardless of what that name is?

borkdude23:11:15

that seems correct. clj also handles:

(defprotocol IBox
  (setVal [x x])
  (getVal [_]))
well, I'm surprised 🙂

borkdude23:11:06

if this is a bug, I'd be happy to post an issue. if it isn't a bug, a compiler warning would be appropriate maybe?

borkdude23:11:08

Something like:

cljs.user=> (defrecord Foo [x x])
Unexpected error (Error) macroexpanding cljs.core$macros/case at (<cljs repl>:1:1).
Duplicate case test constant ':x' on line 1

borkdude23:11:39

CLJ gives feedback about duplicate names in deftype, CLJS doesn't