Sum data type: Difference between revisions
Rename Perl 6 -> Raku, alphabetize, minor clean-up
SqrtNegInf (talk | contribs) (Added Perl example) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 15:
{{Template:See also lists}}
<br><br>
=={{header|ALGOL 68}}==
Line 283 ⟶ 282:
Yes Alliterative
Yes Palindromic</pre>
=={{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 -> $signal {▼
say message $signal;▼
}</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|Phix}}==
Line 339 ⟶ 311:
x = "::c01e:fc9a" -- fine
x = -1 -- error</lang>
(formerly 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 -> $signal {
▲ say message $signal;
▲}</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}}==
|