role Metamodel::TypePretense
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.