Named parameters: Difference between revisions
m
→{{header|Ecstasy}}
(Replace println() with print(); replace output "syntaxhighlight" tag with "pre" tag) |
|||
Line 588:
A common example of using named arguments is a "wither" method:
<syntaxhighlight lang="java">
module NamedParams {
{▼
▲ Point with(Int? x=Null, Int? y=Null)
return new Point(x ?: this.x, y ?: this.y);
}
@Inject Console console;
void run() {
Point origin = new Point(0, 0);
console.print($"origin={origin}");
Point moveRight = origin.with(x=5);
console.print($"moveRight(x=5)={moveRight}");
Point moveUp
console.print($"moveUp(y=3)={moveUp}");
}
}
</syntaxhighlight>
|