jsonnet icon indicating copy to clipboard operation
jsonnet copied to clipboard

A (un)splat operator for named arguments

Open copumpkin opened this issue 8 years ago • 12 comments

What I'd like is for it to behave just like a regular function application, so:

local foo(x, y=100, z) = { x: x, y: y, z: z }

local bar = {
  x: 1,
  y: 2
}

foo(**bar) # Should return { x: 1, y: 100, z: 2 }

Going the other way would also be nice:

local foo(**kwargs) = kwargs

foo(x=6, y=1) # Should return { x: 6, y: 1 }

copumpkin avatar Jun 02 '16 22:06 copumpkin

I've been wondering whether to just do Python semantics *args **kwargs or to instead have a ... keyword that encompasses both and is a bit cleaner.

sparkprime avatar Jun 03 '16 02:06 sparkprime

My general preference is for cleanliness over similarity to python, but others might feel differently 😄

copumpkin avatar Jun 03 '16 13:06 copumpkin

The ... syntax will only cover declaring various forms of varargs, while (as I understood it) this FR was primarily about invoking a function with a programmatically constructed list of (possibly keyword) arguments.

igorpeshansky avatar Jun 03 '16 15:06 igorpeshansky

@igorpeshansky it's both, but yes, I certainly care about invoking more (since that's the one that's harder right now)

copumpkin avatar Aug 01 '16 16:08 copumpkin

I may be missing something, but how would the ... keyword be used to invoke a function?

igorpeshansky avatar Aug 01 '16 16:08 igorpeshansky

I don't understand your examples at all, what programming language are they even in and why should z be 2?

fkz avatar Aug 09 '16 19:08 fkz

arg sorry, I somehow got linked here from nix and thought I'm still in the nix thread...

fkz avatar Aug 09 '16 19:08 fkz

no worries :)

sparkprime avatar Aug 09 '16 20:08 sparkprime

This would be killer for templating out config files....

DanielJoyce avatar Nov 26 '18 16:11 DanielJoyce

Same here, splat operators would be very useful. Do you know of any workaround to call a function dynamically using arguments that are currently available as object fields?

bbinet avatar Nov 29 '18 17:11 bbinet

@bbinet did you ever find a solution?

codesuki avatar Oct 02 '19 07:10 codesuki

This would be quite useful. At the minimum, it would save me from having to wrap the variadic args in [].

ghostsquad avatar Mar 10 '23 21:03 ghostsquad