-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider supporting IDeref "interface" implementation in defrecord #401
Comments
With the following code: (defrecord Example []
clojure.lang.IDeref
(deref [this] :deref)
clojure.lang.IAtom
(reset [this new-value] :reset)
(swap [this f] :swap)
(swap [this f a] :swap)
(swap [this f a b] :swap)
(swap [this f a b args] :swap)
(compareAndSet [this oldv newv] :compare-and-set)) I get this error:
|
Thanks. This example works for me: (defrecord Example []
clojure.lang.IDeref
(deref [this] :deref)
clojure.lang.IAtom
(reset [this new-value] :reset)
(swap [this f] :swap)
#_(swap [this f a] :swap)
#_(swap [this f a b] :swap)
#_(swap [this f a b args] :swap)
#_(compareAndSet [this oldv newv] :compare-and-set))
(prn @(->Example))
(prn (reset! (->Example) 1))
(prn (swap! (->Example) inc)) but there is a problem with multi-arity implementations. Also I didn't add PS: make sure to update both bb and sci and run |
@djblue On the latest bb /sci IDeref branch: https://11627-201467090-gh.circle-artifacts.com/0/release/babashka-0.2.1-SNAPSHOT-macos-amd64.zip (defrecord Example []
clojure.lang.IDeref
(deref [this] :deref)
clojure.lang.IAtom
(reset [this new-value] :reset)
(swap [this f] :swap)
(swap [this f a] :swap)
(swap [this f a b] :swap)
(swap [this f a b args] :swap)
(compareAndSet [this oldv newv] :compare-and-set))
[@(->Example)
(reset! (->Example) 1)
(swap! (->Example) inc)
(swap! (->Example) + 1)
(swap! (->Example) + 1 2)
(swap! (->Example) + 1 2 3)
(compare-and-set! (->Example) 1 2)] $ ~/Downloads/bb-ideref /tmp/foo.clj
[:deref :reset :swap :swap :swap :swap :compare-and-set] |
@borkdude, this works perfectly for me! |
@djblue Now also added |
@djblue has already made a start with this, which is merged to the IDeref branch. One remaining problem is how can we simultaneously have IDeref as a real Java class in sci (probably mainly for supporting
(instance? IDeref x)
and also let people implement it e.g. indefrecord
, like protocols in sci (which are implemented using multimethods).We currently represent protocols as maps with a set of methods in them:
https://github.com/borkdude/babashka/blob/5d9027fe0a41ff1d1e9db124514ad257f1982fe5/src/babashka/impl/protocols.clj#L33
We could add the interface class to that protocol. So when we resolve a class name and we really want a class, but we get a map, we can look in that map and get the class out of it. In other cases where we expect the class to behave more like the protocol we can handle it like the protocol.
The text was updated successfully, but these errors were encountered: