Skip to content

Instantly share code, notes, and snippets.

@postspectacular
Created February 19, 2014 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postspectacular/9096806 to your computer and use it in GitHub Desktop.
Save postspectacular/9096806 to your computer and use it in GitHub Desktop.
deftype & applyTo problem illustrated
(deftype Foo [x y]
clojure.lang.IFn
(invoke [_ a] (case a 0 x, 1 y))
(invoke [_ a nf] (case a 0 x, 1 y nf)))
(def f (Foo. 23 42))
(f 0) ; => 23
(f 2 -1) ; => -1
(apply f [0]) ; AbstractMethodError clojure.core/apply (core.clj:624)
;; with applyTo...
(deftype Foo [x y]
clojure.lang.IFn
(invoke [_ a] (case a 0 x, 1 y))
(invoke [_ a nf] (case a 0 x, 1 y nf))
(applyTo [_ [a nf :as args]]
(if (> 3 (count args))
(case a 0 x, 1 y nf)
(throw (IllegalArgumentException.)))))
(def f (Foo. 23 42))
(apply f [0]) ; => 23
(apply f [2 -1]) ; => -1
;; Question: How to achieve this for CLJS?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment