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
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}}
MODE OPTNAME = STRUCT(STRING name),
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
OPTSPECIES = STRUCT(STRING species),
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
OPTBREED = STRUCT(STRING breed),
Note: this method of implementing Named Parameters is experimental and to date has never been used outside of rosettacode.org.
OWNER=STRUCT(STRING first name, middle name, last name);
<lang algol68>#!/usr/local/bin/a68g --script #
 
# 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),
PROC print petMODE OPTIONS = (FLEX[1:0]UNION(OPTNAME,OPTSPECIES,OPTBREED,OWNER,VOID) option)VOID: (;
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 SPECIESNAME = (STRING speciesname)OPTSPECIESOPTNAME: (OPTSPECIESOPTNAME opt; speciesname OF opt := speciesname; opt),
BREED SPECIES = (STRING breedspecies)OPTBREEDOPTSPECIES: (OPTBREEDOPTSPECIES opt; breedspecies OF opt := breedspecies; opt);,
OP NAME BREED = (STRING namebreed)OPTNAMEOPTBREED: (OPTNAMEOPTBREED opt; namebreed OF opt := namebreed; opt),;
 
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
CASE option[i] IN
(OPTNAMECASE option):[i] name := name OF option,IN
(OPTSPECIESOPTNAME option): speciesname := speciesname OF option,
(OPTBREEDOPTSPECIES option): breedspecies := breedspecies OF option,
(OWNEROPTBREED option): ownerbreed := breed OF option,
(OWNER option): owner := option
ESAC
OD;
printf print(($gx$,"Details: a ",breed, " ", species, " named ",name," owned by ",owner,$l$ newline))
);
 
print pet((NAME "Mike", SPECIES "Dog", BREED "Irish Setter", OWNER("Harry", "S.", "Truman")));
print pet((EMPTY))</lang>
END
</lang>
Output:
<pre>