class Metamodel::EnumHOW
Metaobject representing a Perl 6 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
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
Warning: This class is part of the Rakudo implementation, and is not a part of the language specification.
Methods
method new_type
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
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
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
method export_callback()
Returns the export callback set by .set_export_callback
.
method compose
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
method is_composed()
Returns 1 if the enum is composed, otherwise returns 0.
method compose_values
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
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
method composalizer()
Returns the composalizer set by .set_composalizer
.
method add_enum_value
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
method enum_values()
Returns the values for the enum.
<10 20>;say Numbers.^enum_values; # OUTPUT: {10 => 0, 20 => 1}
method elems
method elems()
Returns the number of values.
<10 20>;say Numbers.^elems; # OUTPUT: 2
method enum_from_value
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
method enum_value_list()
Returns a list of the enum values.
<10 20>;say Numbers.^enum_value_list; # OUTPUT: (10 20)
Type Graph
Routines supplied by role Metamodel::Naming
Metamodel::EnumHOW does role Metamodel::Naming, which provides the following routines:
(Metamodel::Naming) method name
method name()
Returns the name of the metaobject, if any.
say 42.^name; # OUTPUT: «Int»
(Metamodel::Naming) method set_name
method set_name(, )
Sets the new name of the metaobject.
Routines supplied by role Metamodel::AttributeContainer
Metamodel::EnumHOW does role Metamodel::AttributeContainer, which provides the following routines:
(Metamodel::AttributeContainer) method add_attribute
method add_attribute(Metamodel::AttributeContainer: , )
Adds an attribute. $attribute
must be an object that supports the methods name
, type
and package
, which are called without arguments. It can for example be of type Attribute.
(Metamodel::AttributeContainer) method attributes
method attributes(Metamodel::AttributeContainer: )
Returns a list of attributes. For most Perl 6 types, these will be objects of type Attribute.
(Metamodel::AttributeContainer) method set_rw
method set_rw(Metamodel::AttributeContainer: )
Marks a type whose attributes default to having a write accessor. For example in
is rw
The is rw
trait on the class calls the set_rw
method on the metaclass, making all the attributes implicitly writable, so that you can write;
my = Point.new(x => 1, y => 2);.x = 42;
(Metamodel::AttributeContainer) method rw
method rw(Metamodel::AttributeContainer: )
Returns a true value if method set_rw has been called on this object, that is, if new public attributes are writable by default.
Routines supplied by role Metamodel::MethodContainer
Metamodel::EnumHOW does role Metamodel::MethodContainer, which provides the following routines:
(Metamodel::MethodContainer) method add_method
method add_method(Metamodel::MethodContainer: , , )
Adds a method to the metaclass, to be called with name $name
. This should only be done before a type is composed.
(Metamodel::MethodContainer) method methods
method methods(Metamodel::MethodContainer: , :, :)
Returns a list of public methods available on the class (which includes methods from superclasses and roles). By default this stops at the classes Cool, Any or Mu; to really get all methods, use the :all
adverb. If :local
is set, only methods declared directly in the class are returned.
say A.^methods(); # xsay A.^methods(:all); # x infinite defined ...
The returned list contains objects of type Method, which you can use to introspect their signatures and call them.
Some introspection method-look-alikes like WHAT
will not show up, although they are present in any Perl 6 object. They are handled at the grammar level and will likely remain so for bootstrap reasons.
(Metamodel::MethodContainer) method method_table
method method_table(Metamodel::MethodContainer: --> Hash)
Returns a hash where the keys are method names, and the values are methods. Note that the keys are the names by which the methods can be called, not necessarily the names by which the methods know themselves.
(Metamodel::MethodContainer) method lookup
method lookup(Metamodel::MethodContainer: , --> Method)
Returns the first matching method object of the provided $name
or (Mu)
if no method object was found. The search for a matching method object is done by following the mro of $obj
. Note that lookup
is supposed to be used for introspection, if you're after something which can be invoked you probably want to use find_method instead.
say 2.5.^lookup("sqrt").perl: # OUTPUT: «method sqrt (Rat $: *%_) ...»say Str.^lookup("BUILD").perl; # OUTPUT: «submethod BUILD (Str $: :$value = "", *%_ --> Nil) ...»say Int.^lookup("does-not-exist"); # OUTPUT: «(Mu)»
The difference between find_method
and lookup
are that find_method
will use a default candidate for parametric roles, whereas lookup
throws an exception in this case, and that find_method
honors FALLBACK
methods, which lookup
does not.
Routines supplied by role Metamodel::RoleContainer
Metamodel::EnumHOW does role Metamodel::RoleContainer, which provides the following routines:
(Metamodel::RoleContainer) method add_role
method add_role(Metamodel::RoleContainer: , Mu )
Adds the $role
to the list of roles to be composed.
(Metamodel::RoleContainer) method roles_to_compose
method roles_to_compose(Metamodel::RoleContainer: --> List)
returns a list of roles added with add_role
, which are to be composed at type composition time.
Routines supplied by role Metamodel::MROBasedMethodDispatch
Metamodel::EnumHOW does role Metamodel::MROBasedMethodDispatch, which provides the following routines:
(Metamodel::MROBasedMethodDispatch) method find_method
method find_method(, , , *)
Given a method name, it returns the method object of that name which is closest in the method resolution order (MRO). If no method can be found, it returns a VM-specific sentinel value (typically a low-level NULL value) that can be tested for with a test for definedness:
for <upper-case uc># OUTPUT:# method `upper-case` not found# FOO
If :no_fallback
is supplied, fallback methods are not considered.
(Metamodel::MROBasedMethodDispatch) method find_method_qualified
method find_method_qualified(, , )
Given a method name and a type, returns the method from that type. This is used in calls like
self.SomeParentClass::the_method();
(Metamodel::MROBasedMethodDispatch) method can
method can(, )
Returns the list of methods of that name the object can do.
(Metamodel::MROBasedMethodDispatch) method publish_method_cache
Defined as:
method publish_method_cache()
Walk MRO and add methods to cache, unless another method lower in the class hierarchy "shadowed" it.