Metaobject representing a Raku enum.
does Metamodel::Namingdoes Metamodel::Documentingdoes Metamodel::Stashingdoes Metamodel::AttributeContainerdoes Metamodel::MethodContainerdoes Metamodel::MultiMethodContainerdoes Metamodel::RoleContainerdoes Metamodel::BaseTypedoes Metamodel::MROBasedMethodDispatchdoes Metamodel::MROBasedTypeCheckingdoes Metamodel::BUILDPLANdoes Metamodel::BoolificationProtocoldoes Metamodel::REPRComposeProtocoldoes Metamodel::InvocationProtocoldoes Metamodel::Mixins
Warning: this class is part of the Rakudo implementation, and is not a part of the language specification.
Metamodel::EnumHOW
is the metaclass behind the enum
keyword.
<1 2>;say Numbers.HOW ~~ Metamodel::EnumHOW; # OUTPUT: «True»
The following enum declaration:
our Int <Warning Failure Exception Sorrow Panic>;
Is roughly equivalent to this code using Metamodel::EnumHOW
's methods:
BEGIN
method new_type(:!, :?, : = 'P6opaque', :)
Creates a new type object for an enum. $name
is the enum name, $base_type
is the type given when the enum is declared using a scoped declaration (if any), and $repr
is the type representation passed to the enum using the repr
trait. $is_mixin
is unused.
method add_parent(, )
Sets the base type of an enum. This can only be used if no base type was passed to .new_type
.
method set_export_callback(, )
Sets the enum's export callback, which is invoked when calling .compose_values
. This is called when applying the export
trait to an enum. $callback
should be a routine of some sort, taking no arguments, that handles exporting the enum's values.
method export_callback()
Returns the export callback set by .set_export_callback
.
method compose(, :)
Completes a type object for an enum. This is when any roles done by the enum are mixed in. This needs to be called before any enum values can be added using .add_enum_value
.
method is_composed()
Returns 1 if the enum is composed, otherwise returns 0.
method compose_values()
Calls the export callback set by .set_export_callback
and removes it from state. This should be called after adding the enum's values using .add_enum_value
.
method set_composalizer()
Sets the composalizer for an enum, which produces a type that can be mixed in with another. $c
should be a routine of some that has the following signature:
:(, , )
method composalizer()
Returns the composalizer set by .set_composalizer
.
method add_enum_value(, )
Adds a value to this enum. $value
should be an instance of the enum itself, as type Enumeration.
method enum_values()
Returns the values for the enum.
<10 20>;say Numbers.^enum_values; # OUTPUT: {10 => 0, 20 => 1}
method elems()
Returns the number of values.
<10 20>;say Numbers.^elems; # OUTPUT: 2
method enum_from_value(, )
Given a value of the enum's base type, return the corresponding enum.
<10 20>;say Numbers.^enum_from_value(0); # OUTPUT: 10
method enum_value_list()
Returns a list of the enum values.
<10 20>;say Numbers.^enum_value_list; # OUTPUT: (10 20)