Named parameters: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(10 intermediate revisions by 6 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 614 ⟶ 646:
9 gustav
</pre>
=={{header|F Sharp|F#}}==
F# supports named method arguments directly. However they are not supported for functions, function values, or lambda functions.
This example comes from the F# documentation:
<syntaxhighlight lang="fsharp">
type SpeedingTicket() =
member this.GetMPHOver(speed: int, limit: int) = speed - limit
let CalculateFine (ticket : SpeedingTicket) =
let delta = ticket.GetMPHOver(limit = 55, speed = 70)
if delta < 20 then 50.0 else 100.0
let ticket1 : SpeedingTicket = SpeedingTicket()
printfn "%f" (CalculateFine ticket1)
</syntaxhighlight>
=={{header|Factor}}==
Line 899 ⟶ 948:
ve, ho, ul, ur, dl, dr =
border == :round ? ("\u2502","\u2500","\u256d","\u256e","\u2570","\u256f") :
border == :bold ? ("\u2503","\u2501","\u250F","\u2513","\u2517","\u251b") :
border == :double ? ("\u2551","\u2550","\u2554","\u2557","\u255a","\u255d") :
border == :dotted ? ("\u254e","\u254c","\u250c","\u2510","\u2514","\u2518") :
border == :cross ? ("\u2502","\u2500","\u253c","\u253c","\u253c","\u253c") :
("\u2502","\u2500","\u250c","\u2510","\u2514","\u2518")
println(ul, ho^(length(string) + 2padding), ur, "\n",
Line 1,275 ⟶ 1,324:
=={{header|Phix}}==
{{libheader|Phix/basics}}
Phix supports named and optional parameters in a
Optional parameters are
Named parameters can be
Note that low-level builtins (those defined using AutoAsm() in psym.e/syminit()) do not [yet] support named parameters
The classic example (inspired by the standard Python equivalent) is that builtins\timedate.e defines:
Line 1,289 ⟶ 1,338:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #
<span style="color: #008080;">constant</span> <span style="color: #000000;">days</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hours</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">7</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: #000000;">slipup</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;">-- === timedelta([weeks:=]iff(equal(days,4)?true:false))
-- with error if no local/in scope identifier days exists.</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 (not clear whether days:=3 or minutes:=3)
-- 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: #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>-->
Note that pwa/p2js automatically maps named parameters to positional parameters for you, since JavaScript does not support named parameters.
{{out}}
Line 1,802 ⟶ 1,846:
example(grill: "lamb kebab", bar: 3.14)</syntaxhighlight>
{{omit from|Rust}}
Line 2,061 ⟶ 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="
if (!(name is Map && name["first"] != null && name["last"] != null)) {
Fiber.abort("Argument must be a map with keys \"first\" and \"last\"")
Line 2,098 ⟶ 2,170:
{{omit from|bc}}
{{omit from|dc}}
{{omit from|GUISS}}
{{omit from|Joy}}
|