Fork me on GitHub



måning down t'common

💙 2
🔔 1

Madainn mhath


What's the current thinking on keywords arguments or passing the entire map to a function, i.e., (foo :a 1 :b 2 :c 3) vs (foo {:a 1 :b 2 :c 3})? On the clojure destructuring webpage, it says that keyword arguments fall in and out of fashion over the the years. Do you have a preference?


My preference is to always use a map

👍 5

To me it doesn't make much sense to add a lot of fuzz to save two characters


Given my limited time with Clojure, my perference is also to use a map, since as I was (still am) learning clojure, I didn't come across the [& ....] variadic form at that time. So I learnt just to pass maps back and forth. Feels more natural to me 🙂


I'm curious if the 1.11 changes will make it more common to see the variable arity version, since it makes it easier to use and saves you from having to add a separate arity to call without any options. I tend to prefer the variable arity [x y & {:as opts}] or even better [x y & {:keys [foo bar]} so you can see the options in the arglists. But it only really makes sense if the options are truly optional - so for small utility functions mostly. If you expect a certain shaped map then better to just pass a map IMO


How I see it is the other way around: the change in 1.11 allows you to pass maps to named arg functions now. So you can use maps everywhere

💡 4

@U04V15CAJ thx. I hadn't properly understood that


I'd now mostly do & :keys, just because clojure 1.11. Especially for opts.

Ben Sless17:04:07

I want the compiler to figure it out and save on making and taking apart the map to begin with if I go for keyword args. So I'll just use a map


Here’s perhaps a more nuanced answer. If I were to develop some fns to be used from the repl which took a couple of args, then perhaps it’d be nice to be able to call it like:

(foo :some-arg 'bar :some-other-arg 'baz)
That would relieve me of having to remember the argument order. If, however the fn took some kind of options-map, I’d most likely be more happy passing the options-map as, well, a map. Pomegranate has some examples where this all went terribly wrong, eg the lack of documentation from the args vector in add-dependencies


But of course, that fn wouldn’t have been much better if it were defined as

(defn add-dependencies [args] ...)


For this reason, we tend to destructure the args to show which pieces of the map are interesting for the fn:

(defn add-dependencies [{:keys [classloader coordinates repositories] :as args}] ...)

👍 3

The perhaps sad bit about this is that IIRC, destructuring is quite a bit slower than doing (get args :classloader) or some equivalent.


Always maps for me, too. I was trying to conjure up an argument about maps being passed around in general Clojure code, bu t I don’t really know if it holds or if it’s just the convention I’m used to.


Only tangentially related, but there's a subtle point there. Destructing in your function arguments is documentation. If you want args to be generated opaquely and not allow arbitrary map creation then destructuring in a let inside the function allows you to document that the value is opaque.


I’m with you both on destructuring as documentation btw 👍👌

💯 2

I’ve occasionally used :as x to give x a (documenting) name in the destructuring function, even though I haven’t used x in the function body, only its keys.


When questions like this come up, I consider scraping the "core" codebases (eg clojure itself, tools deps, etc) to get answers.


We tend to use :as x for documentation purposes, even if x is not used in the fn-body. But at other times, it’s nice to be able to pass x on to some other fn which might do other stuff with it.


@U09LZR36F I would be careful with looking at core for inspiration. I believe @U072WS7PE has made some tweets around the fact that core is written with different tradeoffs in mind than what you’d probably choose for your app.


He has, I was thinking in terms of API rather than implementation. :arglists, comments, docs, etc. For example, you could override :arglist manually to include destructuring as documentation, but use get in your function for performance. But the key question to answer here is what the user sees when looking up documentation.


But then you’d have to keep the :arglists up to date as the fn evolves, since I guess the :arglist has no functional implications (I haven’t used that at all)


And if there’s one thing I hate more than code that’s hard to read, it’s documentation that’s out of date.


Core does it in a few places where the API is not possible to express, but people know what you mean.


defn might do it, even.


fdecl is manually parsed, because the arglists don't actually work the way they're documented.


Indeed, arglists sometimes uses an informal regex notation, which makes it actually unsuited to do anything serious with it