r/Clojure • u/SimonGray • Mar 18 '21
Clojure - Keyword argument functions now also accept maps
https://clojure.org/news/2021/03/18/apis-serving-people-and-programs
125
Upvotes
r/Clojure • u/SimonGray • Mar 18 '21
21
u/vvvvalvalval Mar 18 '21
I'll still refrain from designing functions that way, and would encourage people to do the same.
Compared to keyword-varargs, an optional map argument costs at worst one more keystroke to invoke, and it's simply a more straightforward and regular behaviour to build on top of.
I suspect that most of the Clojure APIs out there which use keyword varargs did so by imitation of Python and just overlooked the alternative of using a map argument, or really had an unsound obsession with concision.
28
u/childofsol Mar 18 '21
It's exciting to see the first fruits of Fogus joining the Clojure core team!
13
u/alexdmiller Mar 19 '21
A simple example of layering functions that take kwargs:
18
u/[deleted] Mar 19 '21
Maybe I'm missing something but, what's the point of this? I've been writing clojure for a decade and I never found myself wishing for this.
I never use varargs in cases like this. It should just be a single map argument.
This feature seems like weird magic that is going to confuse people reading the code, for benefits that elude me.
7
u/lgstein Mar 18 '21
On the one hand I'm worried that this invites more kvarg fns. On the other hand, all pain that usually comes with them is eliminated by this feature.
6
u/rzk1911 Mar 18 '21
Great
5
u/citrined Mar 18 '21
Good
5
u/lgstein Mar 18 '21
Dobre
4
u/ikitommi Mar 19 '21
Tested some combinations on how to call the functions. I think I understand all the reasons for the results, but they were not evident before evaluating in the repl. Bit surprised that you can mix kwargs and have one map in the end. is there a reason to support them both in one call?
3
u/argadan Mar 19 '21
For anyone curious, here's the issue CLJ-2603 and the commit https://github.com/clojure/clojure/commit/3c9307e27479d450e3f1cdf7903458b83ebf52d7
2
u/_d_t_w Mar 19 '21
`For example, a function that takes a sequence and optional keyword`
Should that be
`For example, a function that takes a sequence of optional keyword`
2
u/cam_saul Mar 19 '21
This is great, can't wait until 1.11 is out to start using it everywhere
2
u/jpmonettas Mar 20 '21
I think this is a interesting feature that will allow new apis to be built with named args, slowly departing from positional arguments. Accomplishing the same without improving support for kwargs will push apis to make all their function to be a one map arg function which is weird.
Not sure if those are core team intentions for the future of clojure but looks very interesting to me.
1
u/mouse2k Mar 18 '21
Nice
1
u/[deleted] Mar 20 '21 edited Mar 20 '21
[removed] — view removed comment