Named parameters: Difference between revisions

→‎{{header|AppleScript}}: Corrected existing example to return the claimed results. Added alternative version. Expounded on handlers with labeled parameters.
(→‎{{header|AppleScript}}: Corrected existing example to return the claimed results. Added alternative version. Expounded on handlers with labeled parameters.)
Line 73:
set lastName to x's lastName
end try
 
return firstName & ", " & lastName
end getName</lang>
Examples:
Line 79 ⟶ 81:
getName({lastName:"Doe"})
--> Returns: "?, Doe"</lang>
----
An easier way to achieve the above is by concatenating a record containing default values for the expected properties to the record actually passed. The result will contain the labels and values from both records, except that where the same label exists in both records, there'll only be one instance in the result and its value will be that from the first record:
 
<lang applescript>on getName(x) -- x assumed to be a record for this demo.
set x to x & {firstName:"?", lastName:"?"}
return x's firstName & ", " & x's lastName
end getName
 
getName({lastName:"Doe"})
--> "?, Doe"</lang>
 
One of AppleScript's handler types has long been "handlers with labeled parameters". These haven't proved particularly popular as they have a limited set of AppleScript-defined labels, which can require some creativity when choosing one to make sense in the script narrative. However, labelled parameters can be given in any order in calls:
 
<lang applescript>on getName from firstName beside lastName
return firstName & ", " & lastName
end getName
 
getName beside "Doe" from "John"
--> "John, Doe"</lang>
 
However, it's also possible to define user labels, with the same flexibility of order, using a slightly different syntax:
 
<lang applescript>on getName given firstName:firstName, lastName:lastName
return firstName & ", " & lastName
end getName
 
getName given lastName:"Doe", firstName:"John"
--> "John, Doe"</lang>
 
AppleScript-defined and user labels can be combined, but the AppleScript ones must be given first if used. The <tt>given</tt> syntax also has a neat feature whereby if the value to be passed is a boolean, the keyword <tt>with</tt> or <tt>without</tt> can be used instead in the call. In fact the compiler imposes this anyway when a boolean is hard-coded into the call:
 
<lang applescript>on getName given firstName:firstName, lastName:lastName, comma:comma
if (comma) then
return firstName & ", " & lastName
else
return firstName & space & lastName
end if
end getName
 
getName given lastName:"Doe", firstName:"John" comma:true -- compiles as: getName with comma given lastName:"Doe", firstName:"John"
--> "John, Doe"
getName without comma given lastName:"Doe", firstName:"John"
--> "John Doe"</lang>
 
Since Mac OS X 10.10, it's been possible to make labelled parameters optional by defining default values for them in the handler declarations. But at least one parameter must be provided in any call:
 
<lang applescript>use AppleScript version "2.4" -- Mac OS X 10.10 (Yosemite) or later.
 
on getName under category : "Misc: " given firstName:firstName : "?", lastName:lastName : "?", comma:comma : true
if (comma) then
return category & firstName & ", " & lastName
else
return category & firstName & space & lastName
end if
end getName
 
getName given lastName:"Doe"
--> "Misc: ?, Doe"</lang>
 
=={{header|Applesoft BASIC}}==
557

edits