A type defines a new object by creating a type object that provides an interface to create instances of objects or to check values against. Any type object is a subclass of Any or Mu. Introspection methods are provided via inheritance from those base classes and the introspection postfix .^. A new type is introduced to the current scope by one of the following type declarators at compile time or with the meta object protocol at runtime. All type names must be unique in their scope.
If no type is provided by the user Perl 6 assumes the type to be Any. This includes containers, base-classes, parameters and return types.
For containers the default type is Any but the default type constraint is Mu. Please note that binding replaces the container, not just the value. The type constraint may change in this case.
.DEFINITE will return True if the invocant is an instance. If it returns False, then the invocant is a type object.
Undefined objects maintain type information in Perl 6. Type objects are used to represent both undefinedness and the type of the undefined value. To provide a general undefined value use Any. If differentiation from Any, the default type for containers and arguments, is required use Mu.
Instances of objects created by .CREATE are by convention defined. The method .defined will return Bool::True to indicate definedness. The exceptions to that rule are Nil and Failure. Please note that any object is able to overload .defined and as such can carry additional information. Also, Perl 6 makes a clear distinction between definedness and trueness. Many values are defined even though they carry the meaning of wrongness or emptiness. Such values are 0, Bool::False, () (empty list) and NaN.
Values can become undefined at runtime via mixin.
To test for definedness call .defined, use //, with/without and signatures.
Turning one type into another is done with coercion methods that have the same name as the target type. This convention is made mandatory by Signatures. The source type has to know how to turn itself into the target type. To allow built-in types to turn themselves into user defined types use augment or the MOP.
Perl 6 provides methods defined in Cool to convert to a target type before applying further operations. Most built-in types descend from Cool and as such may provide implicit coercion that may be undesired. It is the responsibility of the user to care about trap-free usage of those methods.
Type declarators introduce a new type into the given scope. Nested scopes can be separated by ::. New packages are created automatically if no such scope exists already.
Forward declarations can be provided with a block containing only .... The compiler will check at the end of the current scope if the type is defined.
The class declarator creates a compile time construct that is compiled into a type object. The latter is a simple Perl 6 object and provides methods to construct instances by executing initializers and sub methods to fill all attributes declared in a class, and any parent class, with values. Initializers can be provided with the declaration of attributes or in constructors. It's the responsibility of the Metamodel::ClassHOW to know how to run them. This is the only magic part of building objects in Perl 6. The default parent type is Any, which in turn inherits from Mu. The latter provides the default constructor .new which is named like this by convention. Aside from this, .new does not carry any special meaning nor is treated in any special way.
For more information how to use classes see the Classes and objects tutorial.
The type introduced by class can be extended with infix:<but> at runtime. The original type is not modified, instead a new type object is returned and can be stored in a container that type checks successful against the original type or the role that is mixed in.
To test if a given type object is a class, test the meta object method .HOW against Metamodel::ClassHOW.
Private attributes are addressed with any of the twigils $!, @! and %!. They do not have public accessor methods generated automatically. As such they can not be altered from outside the class they are defined in.
The method declarator defines objects of type Method and binds them to the provided name in the scope of a class. Methods in a class are has scoped by default. Methods that are our scoped are not added to the method cache by default and as such can not be called with the accessor sigil $.. Call them with their fully qualified name and the invocant as the first argument.
A normal method in a subclass does not compete with multis of a parent class.
To explicitly state that a method is not a multi method use the only method declarator.
The submethod BUILD is (indirectly) called by .bless. It is meant to set private and public attributes of a class and receives all names attributes passed into .bless. The default constructor .new defined in Mu is the method that invokes it. Given that public accessor methods are not available in BUILD, you must use private attribute notation instead.
A method with the special name FALLBACK will be called when other means to resolve the name produce no result. The first argument holds the name and all following arguments are forwarded from the original call. Multi methods and sub-signatures are supported.
Some built-in introspection methods are actually special syntax provided by the compiler, namely WHAT, WHO, HOW and VAR. Declaring methods with those names will silently fail. A dynamic call will work, what allows to call methods from foreign objects.
Any our scoped method will be visible in the package scope of a class.
Since $.i method call is named i and the attribute is also named i, Perl 6 lets us shortcut. The same applies to :$var, :$!private-attribute, :&attr-with-code-in-it, and so on.
Marks a List method to indicate to hyperoperator to not descend into inner Iterables to call this method. This trait generally isn't something end users would be using, unless they're subclassing or augmenting core List type.
In order to demonstrate the difference consider the following examples, the first using a method (elems) that is nodal and the second using a method (Int) which is not nodal.
The trait handles applied to an attribute of a class will delegate all calls to the provided method name to the method with the same name of the attribute. The object referenced by the attribute must be initialized. A type constraint for the object that the call is delegated to can be provided.
Instead of a method name, a Pair (for renaming), a list of names or Pairs, a Regex or a Whatever can be provided. In the latter case existing methods, both in the class itself and its inheritance chain, will take precedence. If even local FALLBACKs should be searched, use a HyperWhatever.
The trait is accepts a type object to be added as a parent class of a class in its definition. To allow multiple inheritance the trait can be applied more than once. Adding parents to a class will import their methods into the target class. If the same method name occurs in multiple parents, the first added parent will win.
If no is trait is provided the default of Any will be used as a parent class. This forces all Perl 6 objects to have the same set of basic methods to provide an interface for introspection and coercion to basic types.
The trait is rw on a class will create writable accessor methods on all public attributes of that class.
Marks a class or roles attribute as required. If the attribute is not initialized at object construction time throws X::Attribute::Required.
The trait hides provides inheritance without being subject to re-dispatching.
The trait is hidden allows a class to hide itself from re-dispatching.
To allow one class to access the private methods of another class use the trait trusts. A forward declaration of the trusted class may be required.
To add methods and attributes to a class at compile time use augment in front of a class definition fragment. The compiler will demand the pragmas use MONKEY-TYPING or use MONKEY early in the same scope. Please note that there may be performance implications, hence the pragmas.
There are few limitations of what can be done inside the class fragment. One of them is the redeclaration of a method or sub into a multi. Using added attributes is not yet implemented. Please note that adding a multi candidate that differs only in its named parameters will add that candidate behind the already defined one and as such it won't be picked by the dispatcher.
Versioning and authorship can be applied via the adverbs :ver<> and :auth<>. Both take a string as argument, for :ver the string is converted to a Version object. To query a class version and author use .^ver and ^.auth.
Roles are class fragments, which allow the definition of interfaces that are shared by classes. The role declarator also introduces a type object that can be used for type checks. Roles can be mixed into classes and objects at runtime and compile time. The role declarator returns the created type object thus allowing the definition of anonymous roles and in-place mixins.
Use ... as the only element of a method body to declare a method to be abstract. Any class getting such a method mixed in has to overload it. If the method is not overloaded before the end of the compilation unit X::Comp::AdHoc will be thrown.
A role can be used instead of a class to create objects. Since roles can't exist at runtime, a class of the same name is created that will type check successful against the role.
The trait does can be applied to roles and classes providing compile time mixins. To refer to a role that is not defined yet, use a forward declaration. The type name of the class with mixed in roles does not reflect the mixin, a type check does. If methods are provided in more than one mixed in role, the method that is defined first takes precedence. A list of roles separated by comma can be provided. In this case conflicts will be reported at compile time.
For runtime mixins see but and does.
Roles can be provided with parameters in-between  behind a roles name. Type captures are supported.
Parameters can have type constraints, where clauses are not supported for types but can be implemented via subsets.
Default parameters can be provided.
Roles can be used as type constraints wherever a type is expected. If a role is mixed in with does or but, its type-object is added to the type-object list of the object in question. If a role is used instead of a class (using auto-punning), the auto-generated class' type-object, of the same name as the role, is added to the inheritance chain.
Versioning and authorship can be applied via the adverbs :ver<> and :auth<>. Both take a string as argument, for :ver the string is converted to a Version object. To query a role's version and author use .^ver and ^.auth.
Enumerations provide constant key-value-pairs with an associated type. Any key is of that type and injected as a symbol into the current scope. If the symbol is used, it is treated as a constant expression and the symbol is replaced with the value of the enum-pair. Any Enumeration inherits methods from the role Enumeration. Complex expressions for generating key-value pairs are not supported. In general, an enum is a Map whose elements have the Enumeration role mixed in; this role includes, for each element, an index which creates an order on the map.
Stringification of the symbol, which is done automatically in string context and is exactly equal to its name, which is also the key of the enum-pair.
Comparing symbols will use type information and the value of the enum-pair. As value types Num and Str are supported.
All keys have to be of the same type.
However, while you can use directly enums that follow the bare-identifier syntax (as above), you always need to qualify this kind of enum values.
If no value is given Int will be assumed as the values type and incremented by one per key starting at zero. As enum key types Int, Num, Rat and Str are supported.
A different starting value can be provided.
Enums can also be anonymous, with the only difference with named enums being that you cannot use it in Signatures or to declare variables.
There are various methods to get access to the keys and values of the symbols that have been defined. All of them turn the values into Str, which may not be desirable. By treating the enum as a package, we can get a list of types for the keys.
To test if a given type object is an enum, test the meta object method .HOW against Metamodel::EnumHOW or simply test against the Enumeration role.
Returns the list of enum-pairs.
The last example shows what happens if there is no enum-pair that includes that as a value.
Modules are usually one or more source files that expose Perl 6 constructs, such as classes, roles, grammars, subroutines and variables. Modules are usually used for distributing Perl 6 code as libraries which can be used in another Perl 6 program.
For a full explanation see Modules.
Versioning and authorship can be applied via the adverbs :ver<> and :auth<>. Both take a string as argument, for :ver the string is converted to a Version object. To query a modules version and author use .^ver and ^.auth.
Packages are nested namespaces of named program elements. Modules, classes and grammars are all types of package.
For a full explanation see Packages.
Grammars are a specific type of class intended for parsing text. Grammars are composed of rules, tokens and regexes which are actually methods, since grammars are classes.
For a full explanation see Grammars.
Versioning and authorship can be applied via the adverbs :ver<> and :auth<>. Both take a string as argument, for :ver the string is converted to a Version object. To query a grammars version and author use .^ver and ^.auth.
A subset declares a new type that will re-dispatch to its base type. If a where clause is supplied any assignment will be checked against the given code object.
# Only a List with the first element being an Int and the second a Str will pass the type check.
Subsets can be anonymous, allowing inline placements where a subset is required but a name is neither needed nor desirable.
Subsets can be used to check types dynamically, which can be useful in conjunction with require.
From Language/typesystem.pod6 at perl6/doc on GitHub, commit 1b138b3d.