4

I have some code in cljc files which compiles to both Clojure and ClojureScript.

in protocols.cljc

(defprotocol Transformable ".." 
    (scale [this scalar] "" ) ...)

in pattern.cljc

(defrecord APattern [paths]
    Transformable
      (scale [this s] (...)) ...)

in another.cljc

(defn f [pattern] (.scale pattern (/ 1 2)) )

And in core.cljs

(another/f pattern)

However, I'm getting an error on the browser console

TypeError: pattern.scale is not a function

Checking the fields of pattern object in core.cljs (using js-keys) shows me that the object has something called

"patterning$protocols$Transformable$scale$arity$2"

which looks like my function. So am I just doing something wrong to access it in ClojureScript? Does . notation not work? Do I need to do something else?

interstar
  • 26,048
  • 36
  • 112
  • 180

1 Answers1

2

Calls to protocol functions are just like calls to any other function. So your f function should look like:

(require 'protocols)
(defn f [pattern] (protocols/scale pattern (/ 1 2)) )
DanLebrero
  • 8,545
  • 1
  • 29
  • 30
  • The problem I have is that I'm trying to write a library that's usable both in the browser from ClojureScript AND callable from Java. The reason I moved to defprotocol and the .notation is that I was hoping to be able to make Java-friendly APIs without having to write a wrapper. Are you saying thats not possible? – interstar Jan 27 '16 at 13:48
  • Also when I try a version of your suggestion (in the cljs (protocols/scale pattern 0.5) , it now says "p1__25094_SHARP_.scale is not a function" – interstar Jan 27 '16 at 13:51
  • The function `f` is a Clojure function so if you want to use it from Java, you are already in clojureland, so the protocols/scale is the correct way to use it. If what you want to reuse is the Pattern record from Java, then you will use the Java syntax: `new pattern.APattern("").scale("bar");` which is completely unrelated to the function `f` – DanLebrero Jan 28 '16 at 22:48
  • 1
    Also, `(println (protocols/scale (pattern/->APattern "") ""))` works as expected both in ClojureScript and Clojure. Please share your code if you want some help finding what is wrong with your code. – DanLebrero Jan 28 '16 at 22:51