Named parameters: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Julia}}: Question mark needs now space on both sides)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 3 users not shown)
Line 582:
? printName(["first" => "John", "last" => "Doe"])
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.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>
 
{{out}}
<pre>
origin=(x=0, y=0)
moveRight(x=5)=(x=5, y=0)
moveUp(y=3)=(x=5, y=3)
</pre>
 
=={{header|Elixir}}==
Line 1,292 ⟶ 1,324:
=={{header|Phix}}==
{{libheader|Phix/basics}}
Phix supports named and optional parameters in a very simple, natural, and intuitive way, erring on the side of caution when faced with any potential ambiguity.<br>
Optional parameters are specified simply byany providingwith a default, and any non-defaulted parameters must occur before (to the left of) any defaulted parameterssuch.<br>
Named parameters can be given (when invoking a routine)provided in any order, but must be grouped together after (to the right of) any non-namedpositional parameters.<br>
Note that low-level builtins (those defined using AutoAsm() in psym.e/syminit()) do not [yet] support named parameters (maybe one day..), but everything else does.
 
The classic example (inspired by the standard Python equivalent) is that builtins\timedate.e defines:
Line 1,307 ⟶ 1,339:
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">fourdaysdays</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8000000;">timedelta1</span><span style="color: #0000FF;">(,</span> <span style="color: #000000;">dayshours</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">47</span><span style="color: #0000FF;">),</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">1fourdays</span> <span style="color: #0000FF;">,=</span> <span style="color: #0080007060A8;">"wrong = %s\n"timedelta</span><span style="color: #0000FF;">,{(</span><span style="color: #7060A8000000;">elapseddays</span><span style="color: #0000FF;">(:=</span><span style="color: #000000;">oneday4</span><span style="color: #0000FF;">)}),</span>
<span style="color: #000080;font-style:italic;">-- fourdays = timedelta(0,4) -- equivalent, **NB** a plain '=' is a very different thing:</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">onedayslipup</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">days</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- equivalent to=== timedelta([weeks:=]iff((equal(days,4)?true:false))
-- - with an error if no local/in scope variableidentifier days exists.</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">shift</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hours</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">hours</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- perfectly valid (param hours:=local hours)
-- timedelta(0,hours:=15,3) -- illegal (it is not clear whether you meant days:=3 or minutes:=3)</span>
-- though of course the weeks:=0 part is fine</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"fourdays = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fourdays</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"wrong = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">slipup</span><span style="color: #0000FF;">)})</span>
<span style="color: #000080;font-style:italic;">-- **NB** a plain '=' is a very different thing:</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">days</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">oneday</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">days</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- equivalent to timedelta([weeks:=]iff((equal(days,4)?true:false))
-- - with an error if no local variable days exists.</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"wrong = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">oneday</span><span style="color: #0000FF;">)})</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">hours</span><span style="color: #0000FF;">=</span><span style="color: #000000;">7</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">shift</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hours</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">hours</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- perfectly valid (param hours:=local hours)
-- timedelta(0,hours:=15,3) -- illegal (it is not clear whether you meant days:=3 or minutes:=3)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"shift = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">shift</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
Line 2,062 ⟶ 2,094:
whatever bar:=1, baz:=2, foo:=-1, qux:="Why is ev'rybody always pickin' on me?"
End Sub</syntaxhighlight>
 
=={{header|V (Vlang)}}==
1) Vlang allows for a struct literal to be passed to the function, instead of named parameters.
 
2) Using this style, fields need not appear in the same order as they are declared.
 
3) If one or more fields are omitted, their default values will be used instead.
 
4) The named parameter feature was deliberately omitted, for greater code readability.
 
5) Depending on the situation, variadic and/or sum types can also be considered.
<syntaxhighlight lang="Zig">
struct Params {
a int
b int
c int
}
 
fn a_fn(p Params) int {
return p.a + p.b + p.c
}
 
fn main() {
x := a_fn(Params{a: 1, b: 2, c: 3}) // same order
println("x = ${x}")
y := a_fn(Params{c: 3, b: 2, a: 1}) // different order
println("y = ${y}")
z := a_fn(Params{c: 2}) // only one field
println("z = ${z}")
}
</syntaxhighlight>
 
{{out}}
<pre>
x = 6
y = 6
z = 2
</pre>
 
=={{header|Wren}}==
Wren doesn't support named parameters as such though they can be simulated using a map.
<syntaxhighlight lang="ecmascriptwren">var printName = Fn.new { |name|
if (!(name is Map && name["first"] != null && name["last"] != null)) {
Fiber.abort("Argument must be a map with keys \"first\" and \"last\"")
9,482

edits