role Metamodel::TypePretense

Metarole for type pretenses
role Metamodel::TypePretense { }

Warning: this role is part of the Rakudo implementation, and is not a part of the language specification.

Any role will type-check as Mu, Any, and Cool, but don't actually have these classes as parents:

class Class { }
role  Role  { }

say Role ~~ Mu;   # OUTPUT: «True␤»
say Role ~~ Any;  # OUTPUT: «True␤»
say Role ~~ Cool; # OUTPUT: «True␤»

say Class.^parents(:all).map(*.^name);     # OUTPUT: «(Any Mu)␤»
say Role.^pun.^parents(:all).map(*.^name); # OUTPUT: «()␤»

Metamodel::TypePretense is the metarole that's responsible for this behavior. Using the metamethods this provides, types can pretend to be other types, i.e. types can type-check as other types. This can be useful when implementing types that should not store parent types through Metamodel::MultipleInheritance, but should still type-check like other types somehow.

All this metarole does is provide an interface for storing type objects in a HOW and provide a default type_check method that allows types to type-check as the types they're pretending to be if no other type-checking behavior for it exists; any other behavior related to type pretenses are left up to the metaclasses that do this metarole to implement themselves.

Because type pretenses are a property of the metaclass for a HOW, not HOWs themselves, the pretend_to_be and pretending_to_be metamethods this metarole provides must be invoked directly through a metaclass or HOW, not with .^ syntax:

say Role.HOW.pretending_to_be.map(*.^name); # OUTPUT: «(Cool Any Mu)»

This metarole is commonly used in combination with Metamodel::MethodDelegation.

Methods

method pretend_to_be

method pretend_to_be(@types)

Makes all types for a type of HOW pretend to be any of the type objects in @types.

method pretending_to_be

method pretending_to_be()

Returns the type objects this type of HOW is pretending to be.

method type_check

method type_check($obj, $checkee)

If $checkee is the same object as $obj or is of any of the types $obj is pretending to be, returns 1, otherwise returns 0.

See Also

role Metamodel::AttributeContainer

Metaobject that can hold attributes

role Metamodel::C3MRO

Metaobject that supports the C3 method resolution order

role Metamodel::Documenting

Metarole for documenting types.

role Metamodel::Finalization

Metaobject supporting object finalization

role Metamodel::MethodContainer

Metaobject that supports storing and introspecting methods

role Metamodel::MethodDelegation

Metarole for delegating method dispatch

role Metamodel::Mixins

Metaobject for generating mixins

role Metamodel::MROBasedMethodDispatch

Metaobject that supports resolving inherited methods

role Metamodel::MultipleInheritance

Metaobject that supports multiple inheritance

role Metamodel::Naming

Metaobject that supports named types

role Metamodel::ParametricRoleGroupHOW

Represents a group of roles with different parameterizations

role Metamodel::ParametricRoleHOW

Represents a non-instantiated, parameterized, role.

role Metamodel::PrivateMethodContainer

Metaobject that supports private methods

role Metamodel::RoleContainer

Metaobject that supports holding/containing roles

role Metamodel::RolePunning

Metaobject that supports punning of roles.

role Metamodel::Stashing

Metarole for type stashes

role Metamodel::Trusting

Metaobject that supports trust relations between types

role Metamodel::Versioning

Metaobjects that support versioning

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.