This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-04-08
Channels
- # admin-announcements (7)
- # aws (5)
- # beginners (37)
- # boot (39)
- # cider (4)
- # clara (2)
- # cljs-dev (32)
- # cljsjs (1)
- # cljsrn (12)
- # clojure (235)
- # clojure-austin (3)
- # clojure-belgium (7)
- # clojure-berlin (11)
- # clojure-dev (36)
- # clojure-france (10)
- # clojure-japan (10)
- # clojure-poland (2)
- # clojure-russia (39)
- # clojure-uk (4)
- # clojurescript (81)
- # code-reviews (9)
- # core-async (6)
- # core-logic (1)
- # datomic (32)
- # editors (7)
- # emacs (1)
- # hoplon (191)
- # jobs-discuss (14)
- # juxt (4)
- # lein-figwheel (4)
- # leiningen (3)
- # off-topic (7)
- # om (49)
- # onyx (34)
- # other-lisps (1)
- # overtone (11)
- # parinfer (1)
- # proton (5)
- # re-frame (11)
- # reagent (12)
- # spacemacs (2)
- # untangled (90)
- # yada (15)
the former has been found to be kind of annoying if you need to call into another layer down with the same style
generally I tend towards passing an explicit map
except if you have an outer-most api layer that humans will likely type at a repl, where I will use the kwargs style
b/c it's good for people
multiple arities can also be good for people (but is more brittle)
You can always do both:
(defn func
([opts] (... opts ...)) ; opts is a hash map
([k v & kvs] (func (apply hash-map k v kvs))))
(off the top of my head, not even typed into a REPL so excuse any errors etc)
the worst of all worlds :)
Hahaha... or the best
It's certainly not something I'd do as a matter of course. It's useful as a way to migrate from one approach to the other when you realize the {:keys [...]}
approach doesn't "scale"
@urbanslug: def as a special form takes only one symbol and has no multiple arities
@urbanslug: if you try, you'll get a RuntimeException: to many arguments
user=> (def test-user free-plan "user") CompilerException java.lang.RuntimeException: Too many arguments to def, compiling: [...]
@urbanslug: (def test-user (def free-plan "string"))
@urbanslug: works, but test-user points to the free-plan, not it's value
@urbanslug: user=> free-plan "string" user=> test-user #'user/free-plan
@urbanslug: user=> (var-get test-user) "string"
@urbanslug: this will help you get the specific value, when you need it
@urbanslug would pretty easy to make a macro called defmulti
or somesuch
sure, poor naming by me
deflots
defdef
I'm sure there's a way better way to write this, but:
product=> (defmacro defdef [val & names] (let [named (map (fn [n] `(def ~n ~val)) names)] `(do ~@named)))
#'product/defdef
product=> (macroexpand '(defdef "hello" foo bar))
(do (def foo "hello") (def bar "hello"))
product=> (defdef "hello!" foo bar)
#'product/bar
product=> foo
"hello!"
product=> bar
"hello!"
Why would you want to do that? (I guess I missed your justification)
No idea - somebody was just asking how to do it