Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named parameters

I have method

def test(String a, String b) { } 

and I would like to call this with a dynamic parameter map. I always though that

test(['1','2']); //valid call 

and also

test([a:'1',b:'2']); //=> does not work 

will work. but it doesn't. So I remembered the spread operator, but can't get it to work....

Is there a way to call a method like the one above with some kind of map as parameter instead of single parameters?

like image 219
rdmueller Avatar asked Dec 22 '12 13:12

rdmueller


People also ask

What are named parameters in Java?

Java doesn't have named parameters. That's why I said this is "the best Java idiom I've seem for simulating keyword arguments". Objective-C's "named parameters" are also less than ideal, since they force a particular ordering. They aren't true keyword arguments like in Lisp or Python.

Why do we use named parameters?

Named arguments can improve the readability and safety of your code.

What are parameter names?

Parameter names identify the local or global parameters that are defined by you or that are specified by the SRC software. The parameter name is a string of alphanumeric characters starting with a letter that does not contain spaces or special characters.

What are named parameters in Python?

Parameters are the input variables bounded by parentheses when defining a function, whereas arguments are the values assigned to these parameters when passed into a function (or method) during a function call.


1 Answers

Shouldn't the method call be test(a:'1', b:'2'); instead of test([a:'1',b:'2']);?

Please check Named parameters here.

like image 151
Tom Avatar answered Oct 15 '22 17:10

Tom