Named parameters: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
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 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,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: #000000004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<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: #008080;">constant</span> <span style="color: #000000;">fourdays</span> <span style="color: #0000FF;">=</span> <span style="color: #0000007060A8;">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;">-- 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: #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;">wrong</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">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: #000000;">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>-->
 
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>
 
Ruby 1.8 and older versions can do the same, but must use the old syntax <code>:name => value</code>.
 
<syntaxhighlight lang="ruby">def example(opts = {})
defaults = {:foo => 0, :bar => 1, :grill => "pork chops"}
opts = defaults.merge(opts)
printf("foo is %s, bar is %s, and grill is %s\n",
opts[:foo], opts[:bar], opts[:grill])
end
 
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="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\"")
Line 2,098 ⟶ 2,170:
{{omit from|bc}}
{{omit from|dc}}
{{omit from|F_sharp}}
{{omit from|GUISS}}
{{omit from|Joy}}
9,476

edits