Sum data type: Difference between revisions
→{{header|Perl 6}}: Add a Perl 6 example
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Add a Perl 6 example) |
|||
Line 161:
let t1 = Node (Leaf 1, Node (Leaf 2, Leaf 3))</lang>
=={{header|Perl 6}}==
Perl 6 doesn't really have Sum Types as a formal data structure but they can be emulated with enums and switches or multi-dispatch. Note that in this case, feeding the dispatcher an incorrect value results in a hard fault; it doesn't just dispatch to the default. Of course those rules can be relaxed or made more restrictive depending on your particular use case.
<lang perl6>enum Traffic-Signal < Red Yellow Green Blue >;
sub message (Traffic-Signal $light) {
with $light {
when Red { 'Stop!' }
when Yellow { 'Speed Up!' }
when Green { 'Go! Go! Go!' }
when Blue { 'Wait a minute, How did we end up in Japan?!' }
default { 'Whut?' }
}
}
my \Pink = 'A Happy Balloon';
for Red, Green, Blue, Pink -> $light {
say message $light;
}</lang>
{{out}}
<pre>Stop!
Go! Go! Go!
Wait a minute, How did we end up in Japan?!
Type check failed in binding to parameter '$light'; expected Traffic-Signal but got Str ("A Happy Balloon")</pre>
=={{header|REXX}}==
|