In Groovy, if I have:
def say(msg = 'Hello', name = 'world') { "$msg $name!" }
And then call:
say() // Hello world! say("hi") // Hi world! say(null) // null world!
Why is the latter interpreted literally as null and does not apply the default value? Does this not override the default assignment of argument values ββto a method? I get that passing null is different from not missing any length of the w / r / t argument.
My problem is that if I now have a method that takes a collection as an argument:
def items(Set<String> items = []) { new HashSet<>(items) }
This will NullPointerException if I call items(null) , but it works fine if I just say items() . In order for this to work correctly, I have to change the line as new HashSet<>(items ?: []) , Which, again, seems to be completely intended to have the default values ββof the method arguments.
What am I missing here?
source share