Named parameters: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: just a few minor cleanups)
(Add Ecstasy example)
Line 582: Line 582:
? printName(["first" => "John", "last" => "Doe"])
? printName(["first" => "John", "last" => "Doe"])
Doe, John</syntaxhighlight>
Doe, John</syntaxhighlight>

=={{header|Ecstasy}}==
Method and function arguments are passed in order, unless argument names are specified by the caller. Both named arguments and default values for arguments are supported. Ordered and named arguments can both be used in the same invocation, but once a named argument is specified, all subsequent arguments in the invocation must also be named.

A common example of using named arguments is a "wither" method:
<syntaxhighlight lang="java">
module NamedParams
{
const Point(Int x, Int y)
{
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.println($"origin={origin}");
Point moveRight = origin.with(x=5);
console.println($"moveRight(x=5)={moveRight}");
Point moveUp = moveRight.with(y=3);
console.println($"moveUp(y=3)={moveUp}");
}
}
</syntaxhighlight>

Output:
<syntaxhighlight>
origin=(x=0, y=0)
moveRight(x=5)=(x=5, y=0)
moveUp(y=3)=(x=5, y=3)
</syntaxhighlight>


=={{header|Elixir}}==
=={{header|Elixir}}==