Enumeration Reference
Working with the role behind the enum type
What it is #
Routines #
Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.
method enums #
method enums()
Returns a Map of enum values. Works both on the enum type and any key.
method key #
An Enumeration property.
method value #
These are Enumeration properties. The value is assigned automatically by the enum type starting at 0. Oðin gets 1 since it is the second in the enum.
method kv #
multi method kv(::?CLASS:D:)
Returns a list with key and value of the enum-pair.
method pair #
method pair(::?CLASS:D:)
Returns it as a Pair.
method CALL-ME #
multi method CALL-ME(|)
Returns an Enumeration instance given an enum value.
method pick #
multi method pick(::?CLASS:U:)
multi method pick(::?CLASS:U: \n)
multi method pick(::?CLASS:D: *@pos)
It works on the defined class, selecting one element and eliminating it.
method roll #
multi method roll(::?CLASS:U:)
multi method roll(::?CLASS:U: \n)
multi method roll(::?CLASS:D: *@pos)
They work on the defined class selecting one or n elements without eliminating them.
method pred #
method pred(::?CLASS:D:)
method succ #
method succ(::?CLASS:D:)
method Numeric #
multi method Numeric(::?CLASS:D:)
Takes a value of an enum and returns it after coercion to Numeric: Note that if the value cannot be coerced to Numeric, an exception will be thrown.
method Int #
multi method Int(::?CLASS:D:)
Takes a value of an enum and returns it after coercion to Int: Note that if the value cannot be coerced to Int, an exception will be thrown.
method Real #
multi method Real(::?CLASS:D:)
Takes a value of an enum and returns it after coercion to Real: Note that if the value cannot be coerced to Real, an exception will be thrown.
method C<===> #
multi infix:<===> (Enumeration:D \a, Enumeration:D \b)
Equality of Enumeration symbols:
Examples, run three ways #
Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.
7 all-differ · 2 doc-drift · 4 no-output · 3 not-runnable · 4 ok
role Enumeration { }Not executed: the documentation states no expected output for this example.
enum norse-gods <Þor Oðin Loki>; my $one-of-them = norse-gods.pick; say $one-of-them.^name; # OUTPUT: «(norse-gods)» say $one-of-them ~~ Enumeration; # OUTPUT: «True»
(norse-gods)
True
norse-gods
True
norse-gods
False
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
class DNA does Enumeration {
my %pairings = %( A => "T",
T => "A",
C => "G",
G => "C" );
method new( $base-pair where "A" | "C" | "G" | "T" ) {
self.bless( key => $base-pair,
value => %pairings{$base-pair});
}
multi method gist(::?CLASS:D:) {
return "$!key → $!value";
}
}
enum Chain ();
constant length = 16;
for <A C G T>.roll( length ) -> $letter {
my DNA $base = DNA.new( $letter );
Chain.HOW.add_enum_value( Chain, $base );
}
for ^length {
my $base = Chain.pick;
say "{$base.key} and {$base.value}";
}Not executed: the documentation states no expected output for this example.
T and A C and G T and A # and so on...
Not executed: the documentation states no expected output for this example.
enum Foo <bar baz>; say baz ~~ Foo; # OUTPUT: «True» say Foo ~~ bar; # OUTPUT: «False»
True
False
Documentation, Rakudo and Raku++ all agree.
enum Norse-gods <Þor Oðin Freija>; say Norse-gods.keys; # OUTPUT: «(Þor Oðin Freija)»
(Þor Oðin Freija)
(Oðin Freija Þor)
(0 1 2)
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
enum Mass ( mg => 1/1000, g => 1/1, kg => 1000/1 ); say Mass.enums; # OUTPUT: «Map.new((g => 1, kg => 1000, mg => 0.001))» say g.enums; # OUTPUT: «Map.new((g => 1, kg => 1000, mg => 0.001))»
Map.new((g => 1, kg => 1000, mg => 0.001))
Map.new((g => 1, kg => 1000, mg => 0.001))
Documentation, Rakudo and Raku++ all agree.
enum Norse-gods <Þor Oðin Freija>; say Freija.key; # OUTPUT: «Freija»
Freija
Documentation, Rakudo and Raku++ all agree.
enum Norse-gods <Þor Oðin Freija>; say Oðin.value; # OUTPUT: «1»
1
Documentation, Rakudo and Raku++ all agree.
say g.kv; # OUTPUT: «(g 1)»
(g 1)
Both engines agree; the documentation states something else. Trust the engines.
say g.pair; # OUTPUT: «g => 1»
g => 1
Both engines agree; the documentation states something else. Trust the engines.
enum Mass ( mg => 1/1000, g => 1/1, kg => 1000/1 ); say Mass(1/1000); # OUTPUT: mg
Not executed: the documentation states no expected output for this example.
say Norse-gods.pick() for ^3; # OUTPUT: «ÞorFreijaOðin»
Neither engine can run this in isolation — the example depends on context from the surrounding text.
say Norse-gods.roll() for ^3; # OUTPUT: «FreijaFreijaOðin»
Neither engine can run this in isolation — the example depends on context from the surrounding text.
say Freija.pred; # OUTPUT: «Oðin»
Oðin
-1
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
say Oðin.succ; # OUTPUT: «Freija»
Freija
1
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
enum Numbers ( cool => '42', almost-pi => '3.14', sqrt-n-one => 'i' ); say cool.Numeric; # OUTPUT: «42» say almost-pi.Numeric; # OUTPUT: «3.14» say sqrt-n-one.Numeric; # OUTPUT: «0+1i»
42
3.14
0+1i
42
3.14
42
3
0
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
enum Numbers ( cool => '42', almost-pi => '3.14', sqrt-n-one => 'i' ); say cool.Int; # OUTPUT: «42» say almost-pi.Int; # OUTPUT: «3» try say sqrt-n-one.Int; say $!.message if $!; # OUTPUT: «Cannot convert 0+1i to Int: imaginary part not zero»
42
3
Cannot convert 0+1i to Int: imaginary part not zero
42
3
Cannot convert string to number: base-10 number must begin with valid digits or '.' in '<HERE>i' (indicated by <HERE>)
42
3
0
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
enum Numbers ( cool => '42', almost-pi => '3.14', sqrt-n-one => 'i' ); say cool.Real; # OUTPUT: «42» say almost-pi.Real; # OUTPUT: «3.14» try say sqrt-n-one.Real; say $!.message if $!; # OUTPUT: «Cannot convert 0+1i to Real: imaginary part not zero»
42
3.14
Cannot convert 0+1i to Real: imaginary part not zero
42
3.14
Cannot convert string to number: base-10 number must begin with valid digits or '.' in '<HERE>i' (indicated by <HERE>)
42
3
0
All three differ. Needs a human.
Not yet examined. Which of these is correct has not been established — do not treat either engine as settled here.
say Norse-gods.pick() === Freija for ^3; # OUTPUT: «FalseFalseTrue»
Neither engine can run this in isolation — the example depends on context from the surrounding text.