I use goog.structs.LinkedMap to implement the cljs.core/IFn protocol, but I can’t use the function form to call an instance of LinkedMap

Viewed 44

here is my code,running on shadow-cljs.

(ns app.main
  (:require [goog.structs.LinkedMap]))
(extend-type goog.structs.LinkedMap
  cljs.core/IFn
  (-invoke
    ([m k] (.get m k nil))
    ([m k not-found] (.get m k not-found))))

(def m (goog.structs.LinkedMap.))
(.set m 34 :foo)
(println (m 34))

The error message is as follows:#object[TypeError TypeError: app.main.m is not a function]

but I ran (type m), the result is as follow:#object[Function], This is indeed a function.

sample code and eval result

1 Answers

Hmm this is indeed a problem. A while ago I added an optimization to make function calls for goog.* code faster and produce less code. However this prevents IFn from working properly for those types. All normal protocols work just fine only IFn is an issue.

I'll think about this and see if I can figure out a way to keep both. Please open a shadow-cljs github issue so I don't forget.

The (type m) will get you the Constructor of m which is indeed a function. m is the instance which is not a function.

Related