Working with the role behind the enum type
This is the role implemented by the enum-pairs in the enum
type. In general, it is used to create constant sets, the elements of which become also constant symbols in the current namespace and to establish a relationship between the symbols belonging to the same set. In general, you will find Enumeration
in enum
type types:
<Þor Oðin Loki>;my = norse-gods.pick;say ~~ Enumeration; # OUTPUT: «True»
but nothing prevents you from using it in your own programs if you want to restrict somehow the relationship between the key and the value:
does Enumeration();constant length = 16;for <A C G T>.roll( length ) ->for ^length
In this code, DNA
consumes the Enumeration
role, which is from this point of view a pair of key and value; we can use the generated DNA
objects to compose an enum
type from which elements can be picked one by one, with the output shown below.
T and AC and GT and A# and so on...
An item would smartmatch the enum class, but not the other way round:
<bar baz>;say baz ~~ Foo; # OUTPUT: «True»say Foo ~~ bar; # OUTPUT: «False»
These are the methods included in this role:
An Enumeration
property.
<Þor Oðin Freija>;say Freija.key; # OUTPUT: «Freija»
These are Enumeration
properties.
<Þor Oðin Freija>;say Oðin.value; # OUTPUT: «1»
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 enums()
Returns a Map of enum values. Works both on the enum type and any key.
( 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))»
multi method kv(::?CLASS:)
Returns a list with key and value of the enum-pair.
say g.kv; # OUTPUT: «(g 1)»
method pair(::?CLASS:)
Returns it as a Pair
.
say g.pair; # OUTPUT: «g => 1»
multi method CALL-ME(|)
Returns an Enumeration
instance given an enum value.
( mg => 1/1000, g => 1/1, kg => 1000/1 );say Mass(1/1000); # OUTPUT: mg
multi method pick(::?CLASS:)multi method pick(::?CLASS: \n)multi method pick(::?CLASS: *)
It works on the defined class, selecting one element and eliminating it.
say Norse-gods.pick() for ^3; # OUTPUT: «ÞorFreijaOðin»
multi method roll(::?CLASS:)multi method roll(::?CLASS: \n)multi method roll(::?CLASS: *)
They work on the defined class selecting one or n
elements without eliminating them.
say Norse-gods.roll() for ^3; # OUTPUT: «FreijaFreijaOðin»
method pred(::?CLASS:)
say Freija.pred; # OUTPUT: «Oðin»
method succ(::?CLASS:)
say Oðin.succ; # OUTPUT: «Freija»
multi method Numeric(::?CLASS:)
Takes a value of an enum and returns it after coercion to Numeric
:
( 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»
Note that if the value cannot be coerced to Numeric
, an exception will be thrown.
multi method Int(::?CLASS:)
Takes a value of an enum and returns it after coercion to Int
:
( 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»
Note that if the value cannot be coerced to Int
, an exception will be thrown.
multi method Real(::?CLASS:)
Takes a value of an enum and returns it after coercion to Real
:
( 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»
Note that if the value cannot be coerced to Real
, an exception will be thrown.
===
multi infix:<===> (Enumeration \a, Enumeration \b)
Equality of Enumeration
symbols:
say Norse-gods.pick() === Freija for ^3; # OUTPUT: «FalseFalseTrue»