NAME

Mu - The root of the cperl type hierarchy

SYNOPSIS

    class MyClass {
      has $a = 1;
    }
    my MyClass $obj = new MyClass;
    print $obj->{a};    # OUTPUT: «1»

DESCRIPTION

Mu provides generic methods for all cperl classes. All classes derive from Mu, the last @ISA entry is always "Mu". The common prefix mu 無 means "not have", "without" in East Asian languages.

METHODS

new CLASSNAME, @ARGS

Creates a new object as avref with the declared number of fields (i.e. number of has declarations, with padix offsets stored in @CLASSNAME::FIELDS).

The array is shaped, and the field entries are initialized from the given positional args, and if missing from the initial class value, or if that is missing as undef.

:const fields are being initialized without problems, :const only means that the method accessor is not :lvalue.

array fields, like has @a are initialized as array refs, has fields, like has %h are initialized as hash refs.

  class XX {
    has @a :const = (0..2);
    has %h :const;
  }
  XX->new([0..4],{key => $val});
CREATE CLASSNAME

Creates a new object as avref with the declared number of fields (i.e. number of has declarations, with padix offsets stored in @CLASSNAME::FIELDS). The array is shaped, but the field entries are left empty.

... Many more methods to come, see https://docs.perl6.org/type/Mu

SUBROUTINES

fields
    method fields (Mu) :Array(fields)

The fields method on a class or object of a class returns the ordered list of all declared has fields of the class. See "CPERL METHODS" in fields on the available methods.

defined ARGUMENT
    multi sub    defined (Mu) :Bool
    multi method defined ()   :Bool

Returns False on the type object, and True otherwise.

    say Int->defined;                # OUTPUT: «False»
    my Int $i = 42;
    say $i->defined;                 # OUTPUT: «True» 

Very few types (like Failure) override defined to return False even for instances:

    sub fails() { die 'oh noe' };
    say fails()->defined;            # OUTPUT: «False» 

... Many more subroutines to come, see https://docs.perl6.org/type/Mu