class Metamodel::EnumHOW

Metaobject representing a Perl 6 enum.

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::InvocationProtocol
    does Metamodel::Mixins
        { }

Metamodel::EnumHOW is the meta class behind the enum keyword.

enum numbers <1 2>;
say numbers.HOW ~~ Metamodel::EnumHOW       # OUTPUT: «True␤» 

Warning: This class is part of the Rakudo implementation, and is not a part of the language specification.

Methods

method add_enum_value

method add_enum_value(Metamodel::ClassHOW:D: $obj$value)

Add a value to this enum.

method enum_values

method enum_values(Metamodel::ClassHOW:D: $obj)

Returns the values for the enum.

enum numbers <10 20>;
say numbers.^enum_values;                   # OUTPUT: {10 => 0, 20 => 1} 

method elems

method elems(Metamodel::ClassHOW:D: $obj)

Returns the number of values.

enum numbers <10 20>;
say numbers.^elems;                         # OUTPUT: 2 

method enum_from_value

method enum_from_value(Metamodel::ClassHOW:D: $obj$value)

Given an value, return the corresponding enum.

enum numbers <10 20>;
say numbers.^enum_from_value(0)             # OUTPUT: 10 

method enum_value_list

method enum_value_list(Metamodel::ClassHOW:D: $obj)

Returns a list of the enum values.

enum numbers <10 20>;
say numbers.^enum_value_list;               # OUTPUT: (10 20) 

Type Graph

Type relations for Metamodel::EnumHOW
perl6-type-graph Metamodel::EnumHOW Metamodel::EnumHOW Any Any Metamodel::EnumHOW->Any Metamodel::Naming Metamodel::Naming Metamodel::EnumHOW->Metamodel::Naming Metamodel::Stashing Metamodel::Stashing Metamodel::EnumHOW->Metamodel::Stashing Metamodel::AttributeContainer Metamodel::AttributeContainer Metamodel::EnumHOW->Metamodel::AttributeContainer Metamodel::MethodContainer Metamodel::MethodContainer Metamodel::EnumHOW->Metamodel::MethodContainer Metamodel::MultiMethodContainer Metamodel::MultiMethodContainer Metamodel::EnumHOW->Metamodel::MultiMethodContainer Metamodel::RoleContainer Metamodel::RoleContainer Metamodel::EnumHOW->Metamodel::RoleContainer Metamodel::BaseType Metamodel::BaseType Metamodel::EnumHOW->Metamodel::BaseType Metamodel::MROBasedMethodDispatch Metamodel::MROBasedMethodDispatch Metamodel::EnumHOW->Metamodel::MROBasedMethodDispatch Metamodel::MROBasedTypeChecking Metamodel::MROBasedTypeChecking Metamodel::EnumHOW->Metamodel::MROBasedTypeChecking Metamodel::BUILDPLAN Metamodel::BUILDPLAN Metamodel::EnumHOW->Metamodel::BUILDPLAN Metamodel::BoolificationProtocol Metamodel::BoolificationProtocol Metamodel::EnumHOW->Metamodel::BoolificationProtocol Mu Mu Any->Mu

Stand-alone image: vector

Routines supplied by role Metamodel::Naming

Metamodel::EnumHOW does role Metamodel::Naming, which provides the following routines:

(Metamodel::Naming) method name

method name($type)

Returns the name of the meta object, if any.

say 42.^name;       # OUTPUT: «Int␤» 

(Metamodel::Naming) method set_name

method set_name($type$new_name)

Sets the new name of the meta object.

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: $obj$name$attribute)

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: $obj)

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: $obj)

Marks a type whose attributes default to having a write accessor. For example in

class Point is rw {
    has $.x;
    has $.y;
}

The is rw trait on the class calls the set_rw method on the meta class, making all the attributes implicitly writable, so that you can write;

my $p = Point.new(x => 1=> 2);
    $p.x = 42;

(Metamodel::AttributeContainer) method rw

method rw(Metamodel::AttributeContainer: $obj)

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: $obj$name$code)

Adds a method to the meta class, to be called with name $name. This should only be done before a type is composed.

(Metamodel::MethodContainer) method methods

method methods(Metamodel::MethodContainer: $obj:$all:$local)

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.

class A {
    method x() { };
}
 
say A.^methods();                   # x 
say 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:D: $obj --> Hash:D)

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: $obj$name --> 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)␤» 

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:D: $objMu $role)

Adds the $role to the list of roles to be composed.

(Metamodel::RoleContainer) method roles_to_compose

method roles_to_compose(Metamodel::RoleContainer:D: $obj --> List:D)

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(Metamodel::MROBasedMethodDispatch:D: $obj$name$no_fallback)

Given a method name, 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> {
    Str.^find_method: $^meth andthen .("foo").say
        orelse "method `$meth` not found".say
}
# 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(Metamodel::MROBasedMethodDispatch:D: $obj$type$name)

Given a method name and a type, returns the method from that type. This is used in calls like

self.SomeParentClass::the_method();