Groovy supports also the usage of methods with default values for parameters.
Ejemplo:
def foo(x=1, y=2) { x+y } assert foo() == 3 assert foo(2) == 4 assert foo(5,10) == 15
Any parameterT t
can have a default value usingT t=x
,
where x
is the value.
In fact this is a short form to declare an
overloaded method.
def foo(p1, p2, ..., pn, T t=x, q1, q2, ..., qm)
becomes a method
def foo(p1, p2, ..., pn, T t, q1, q2, ..., qm)
where t
has no default value and a method
def foo(p1, p2, ..., pn, q1, q2, ..., qm)
with this implementation:
def foo(p1, p2, ..., pn, q1, q2, ..., qm) { foo(p1, p2, ..., pn, x, q1, q2, ..., qm) }
Casiano Rodríguez León