Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: having difficulty applying protocol to 2 types successfully

Other is a more complex version (wrapping) of What. It does what What does but much more. I took care to define 2 namespaces.

(ns what)

(defprotocol IWhatever
  (whatever [this]))

(deftype What []
  IWhatever
  (whatever [this]
            (str "whatever")))

(whatever (->What))

(ns other (:require what))

(deftype Other []
  what/IWhatever
  (whatever [this]
            (what/whatever (what/->What))))

(whatever (->Other)) ;bad line

The error is:

clojure.lang.Compiler$CompilerException: java.lang.RuntimeException: Unable to resolve symbol: whatever in this context, compiling:(C:\...)

Why won't this last expression resolve? It's like the name can't be found, but as you can see I redefined it under the current namespace.

It's nonsense code but I used the simplest example of the problem to illustrate the point. I'm running this in LightTable if that's relevant.

like image 900
Mario Avatar asked May 02 '26 02:05

Mario


1 Answers

Qualify the final line with the namespace of where the protocol was defined. I kept thinking to try to call the whatever method in the other namespace since it was defined there.

(what/whatever (->Other))

Thanks goes to @soulcheck and everyone else who took time to help.

like image 71
Mario Avatar answered May 04 '26 00:05

Mario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!