Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. peterwestmacott [09:55]
  2. does anyone know a quick way to add no-op implementations for every method in a protocol to a record?
  3.  
  4. dominicm [09:56]
  5. ooh, ooh
  6. I've been trolling github to be ready for this moment
  7. and I can't remember the library name, I think it's something about leaves.
  8.  
  9. peterwestmacott [10:01]
  10. 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
  11. ergo: `(extend AType AProtocol (zipmap [fn-names...] (repeat (constantly nil))))`
  12.  
  13. dominicm [10:07]
  14. https://github.com/bguthrie/shrubbery
  15. | bguthrie/shrubbery
  16. | Mocks, stubs, and spies for Clojure protocols.
  17.  
  18. peterwestmacott [10:16]
  19. 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)
  20.  
  21. boot.user=> (constantly nil)
  22. #object[clojure.core$constantly$fn__5472 0x5ac6745c "clojure.core$constantly$fn__5472@5ac6745c"]
  23. boot.user=> ((constantly nil) 1 2 3)
  24. nil
  25. boot.user=> (zipmap [:bar :baz] [1 2])
  26. {:bar 1, :baz 2}
  27. boot.user=> (zipmap [:bar :baz] (repeat (constantly nil)))
  28. {: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"]}
  29. boot.user=> (defprotocol Foo (bar [this]) (baz [this]))
  30. Foo
  31. boot.user=> (defrecord FooRecord [])
  32. boot.user.FooRecord
  33. boot.user=> (->FooRecord)
  34. #boot.user.FooRecord{}
  35. boot.user=> (bar (->FooRecord))
  36.  
  37. boot.user=> java.lang.IllegalArgumentException: No implementation of method: :bar of protocol: #'boot.user/Foo found for class: boot.user.FooRecord
  38.  
  39.  
  40. boot.user=> (extend FooRecord Foo (zipmap [:bar :baz] (repeat (constantly nil))))
  41. nil
  42. boot.user=> (bar (->FooRecord))
  43. nil
  44. boot.user=> (baz (->FooRecord))
  45. nil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement