Named parameters: Difference between revisions
Content deleted Content added
Added a sentence about optional arguments. |
→{{header|ALGOL 68}}: Changes for recent Algol 68G and allow runnning with a wider range of compilers |
||
Line 32:
=={{header|ALGOL 68}}==
{{trans|Lua}}
<lang algol68>BEGIN
MODE OPTNAME = STRUCT(STRING name),▼
OPTSPECIES = STRUCT(STRING species),▼
OPTBREED = STRUCT(STRING breed),▼
OWNER=STRUCT(STRING first name, middle name, last name);▼
# earlier versions of Algol 68G would allow empty options to be specified as () #
▲MODE OPTNAME = STRUCT(STRING name),
# however more recent versions reject that and so we include VOID in the MODEs #
▲ OPTSPECIES = STRUCT(STRING species),
# for options. Empty option lists can be written as (EMPTY) #
▲ OPTBREED = STRUCT(STRING breed),
▲ OWNER=STRUCT(STRING first name, middle name, last name);
# due to the Yoneda ambiguity simple arguments must have an unique operator defined #
# E.g. a string cannot be coerced to a structure with a single string field #
OP NAME = (STRING name)OPTNAME: (OPTNAME opt; name OF opt := name; opt),▼
OP
▲
▲PROC print pet = ([]UNION(OPTNAME,OPTSPECIES,OPTBREED,OWNER) option)VOID: (
PROC print pet = (OPTIONS option)VOID: (
STRING name:="Rex", species:="Dinosaur", breed:="Tyrannosaurus"; # Defaults #
OWNER owner := ("George","W.","Bush");
FOR i TO UPB option DO
(
(
(
(OWNER option): owner := option
ESAC
OD;
);
print pet((NAME "Mike", SPECIES "Dog", BREED "Irish Setter", OWNER("Harry", "S.", "Truman")));
print pet((EMPTY))
END
</lang>
Output:
<pre>
|