Sum data type: Difference between revisions

Add CLU
m (added omits)
(Add CLU)
Line 106:
Character c = C , address of c = 0x7ffd71122354
</pre>
 
=={{header|CLU}}==
<lang clu>start_up = proc ()
% A sum data type is called a `oneof' in CLU.
% (There is also a mutable version called `variant' which works
% the same way.)
irc = oneof[i: int, r: real, c: char]
% We can use the new type as a parameter to other types
ircseq = sequence[irc]
% E.g., fill an array with them
ircs: ircseq := ircseq$[
irc$make_i(20),
irc$make_r(3.14),
irc$make_i(42),
irc$make_c('F'),
irc$make_c('U'),
irc$make_r(2.72)
]
% 'tagcase' is used to discriminate between the various possibilities
% e.g.: iterate over the elements in the array
po: stream := stream$primary_output()
for i: irc in ircseq$elements(ircs) do
tagcase i
tag i (v: int):
stream$putl(po, "int: " || int$unparse(v))
tag r (v: real):
stream$putl(po, "real: " || f_form(v, 1, 2))
tag c (v: char):
stream$putl(po, "char: " || string$c2s(v))
end
end
end start_up </lang>
{{out}}
<pre>int: 20
real: 3.14
int: 42
char: F
char: U
real: 2.72</pre>
 
=={{header|Delphi}}==
2,114

edits