Named parameters: Difference between revisions

m
(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 {
const Point with(Int? x=Null, Int? y=Null) {
{
const Point with(Int? x=Null, Int? y=Null) {
{
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 = moveRight.with(y=3);
console.print($"moveUp(y=3)={moveUp}");
}
}
}
</syntaxhighlight>
 
162

edits