Metamodel::EnumHOW Reference
Metaobject representing a Raku enum.
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 new_type #
method new_type(:$name!, :$base_type?, :$repr = 'P6opaque', :$is_mixin)
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 #
method add_parent($obj, $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 #
method set_export_callback($obj, $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 #
method export_callback($obj)
Returns the export callback set by .set_export_callback.
method compose #
method compose($obj, :$compiler_services)
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 #
method is_composed($obj)
Returns 1 if the enum is composed, otherwise returns 0.
method compose_values #
method compose_values($obj)
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 #
method set_composalizer($c)
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 #
method composalizer($obj)
Returns the composalizer set by .set_composalizer.
method add_enum_value #
method add_enum_value($obj, $value)
Adds a value to this enum. $value should be an instance of the enum itself, as type Enumeration.
method enum_values #
method enum_values($obj)
Returns the values for the enum.
method elems #
method elems($obj)
Returns the number of values.
method enum_from_value #
method enum_from_value($obj, $value)
Given a value of the enum's base type, return the corresponding enum.
method enum_value_list #
method enum_value_list($obj)
Returns a list of the enum values.
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.
8 no-output · 1 rakupp-differs
class Metamodel::EnumHOW
does Metamodel::Naming
does Metamodel::Documenting
does Metamodel::Stashing
does Metamodel::AttributeContainer
does Metamodel::MethodContainer
does Metamodel::MultiMethodContainer
does Metamodel::RoleContainer
does Metamodel::BaseType
does Metamodel::MROBasedMethodDispatch
does Metamodel::MROBasedTypeChecking
does Metamodel::BUILDPLAN
does Metamodel::BoolificationProtocol
does Metamodel::REPRComposeProtocol
does Metamodel::Mixins
{ }Not executed: the documentation states no expected output for this example.
enum Numbers <1 2>; say Numbers.HOW ~~ Metamodel::EnumHOW; # OUTPUT: «True»
True
True
False
Raku++ disagrees with both the documentation and Rakudo — a defect.
our Int enum Error <Warning Failure Exception Sorrow Panic>;
Not executed: the documentation states no expected output for this example.
BEGIN {
my constant Error = Metamodel::EnumHOW.new_type: :name<Error>, :base_type(Int);
Error.^add_role: Enumeration;
Error.^add_role: NumericEnumeration;
Error.^compose;
for <Warning Failure Exception Sorrow Panic>.kv -> Int $v, Str $k {
# Note: Enumeration.pred and .succ will not work when adding enum
# values as pairs. They should be instances of the enum itself, but
# this isn't possible to do without nqp.
Error.^add_enum_value: $k => $v;
OUR::{$k} := Error.^enum_from_value: $v;
}
Error.^compose_values;
OUR::<Error> := Error;
}Not executed: the documentation states no expected output for this example.
:($type, $name, @enum_values)
Not executed: the documentation states no expected output for this example.
enum Numbers <10 20>;
say Numbers.^enum_values; # OUTPUT: {10 => 0, 20 => 1}Not executed: the documentation states no expected output for this example.
enum Numbers <10 20>; say Numbers.^elems; # OUTPUT: 2
Not executed: the documentation states no expected output for this example.
enum Numbers <10 20>; say Numbers.^enum_from_value(0); # OUTPUT: 10
Not executed: the documentation states no expected output for this example.
enum Numbers <10 20>; say Numbers.^enum_value_list; # OUTPUT: (10 20)
Not executed: the documentation states no expected output for this example.