Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- peterwestmacott [09:55]
- does anyone know a quick way to add no-op implementations for every method in a protocol to a record?
- dominicm [09:56]
- ooh, ooh
- I've been trolling github to be ready for this moment
- and I can't remember the library name, I think it's something about leaves.
- peterwestmacott [10:01]
- I think I might just do it myself (lisp curse) - but it doesn’t look too complicated as I know the function names ahead of time
- ergo: `(extend AType AProtocol (zipmap [fn-names...] (repeat (constantly nil))))`
- dominicm [10:07]
- https://github.com/bguthrie/shrubbery
- | bguthrie/shrubbery
- | Mocks, stubs, and spies for Clojure protocols.
- peterwestmacott [10:16]
- oh, and `[fn-names...]` can be replaced by `(-> AProtocol :sigs keys)` for the fully dynamic version - though I worry that I might be leaning on an implementation detail there! (edited)
- boot.user=> (constantly nil)
- #object[clojure.core$constantly$fn__5472 0x5ac6745c "clojure.core$constantly$fn__5472@5ac6745c"]
- boot.user=> ((constantly nil) 1 2 3)
- nil
- boot.user=> (zipmap [:bar :baz] [1 2])
- {:bar 1, :baz 2}
- boot.user=> (zipmap [:bar :baz] (repeat (constantly nil)))
- {:bar #object[clojure.core$constantly$fn__5472 0x6d009328 "clojure.core$constantly$fn__5472@6d009328"], :baz #object[clojure.core$constantly$fn__5472 0x6d009328 "clojure.core$constantly$fn__5472@6d009328"]}
- boot.user=> (defprotocol Foo (bar [this]) (baz [this]))
- Foo
- boot.user=> (defrecord FooRecord [])
- boot.user.FooRecord
- boot.user=> (->FooRecord)
- #boot.user.FooRecord{}
- boot.user=> (bar (->FooRecord))
- boot.user=> java.lang.IllegalArgumentException: No implementation of method: :bar of protocol: #'boot.user/Foo found for class: boot.user.FooRecord
- boot.user=> (extend FooRecord Foo (zipmap [:bar :baz] (repeat (constantly nil))))
- nil
- boot.user=> (bar (->FooRecord))
- nil
- boot.user=> (baz (->FooRecord))
- nil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement