Reputation: 75
I'm completly new to groovy, and to be honest I have real hard time with finding things in groovy. So i noticed that somethink like this can be achieve:
We have function
def static someMethod (params) {
...
}
and then you are able to call this one like this :
someMethod (param1 : value1, param2 : value2, param3 : value3....)
So my question is : how can I read those param1, param2 etc in my someMethod
? I mean should I do something close to this ?
def static someMethod (params) {
def result = param1 + param2 + param3
}
And If someone let say dont give param3 will this function return nullPointerException? Like I said I'm new, so any answer is probably best (some links meaby?) Thanks in advance and sorry for my bad english.
Upvotes: 1
Views: 302
Reputation: 84786
When You declare such method:
def static someMethod (params) {
def result = params.param1 + params.param2 + params.param3
println result
}
and then invoke like this:
someMethod (param1 : 1, param2 : 2, param3 :3)
what You pass to method invocation is map
, so in the method's body there's a need to prefix key names with map name in this case params
.
When no value is present for key - null
will be returned, unless You define a map
using withDefault
- then default value will be returned.
Upvotes: 3