Metaobject that supports low-level type operations
Metamodel::Primitives provides low-level operations for working with types, which are otherwise only available as implementation-dependent directives. These primitives are available as class methods.
Here is an example that steals the metamodel instance from the Int class to create a custom type (usually you would create your own metaclass if you mess with something as low-level), which allows calling of just one method called
my Mu := Metamodel::Primitives.create_type(Int.HOW, 'P6opaque');.^set_name('why oh why?');my = why => sub ($) ;Metamodel::Primitives.install_method_cache(, , :authoritative);.why; # 42.list;CATCH ;# OUTPUT: «X::Method::NotFound: Method 'list' not found for invocant of class 'why oh why?'␤»
method create_type(Mu , = 'P6opaque')
Creates and returns a new type from a metaobject
$how and a representation name.
method set_package(Mu , )
Sets the package associated with the type.
method install_method_cache( Mu , , : = True)
Installs a method cache, that is, a mapping from method names to code objects. If
:authoritative is missing, or set to
True, then calls of methods that do not exist in the cache will throw an exception of type X::Method::NotFound. If
:authoritative is set to
False, the usual fallback mechanism are tried.
method configure_type_checking( Mu , , : = True, : = False )
Configures the type checking for
@cache is a list of known types against which
$type checks positively (so in a classical class-based system, the type itself and all recursive superclasses). If
:authoritative is missing or
True, this type will fail checks against all types not in
:call_accepts is True, the method ACCEPTS will be called for type checks against this type.
method configure_destroy(Mu , )
DESTROY methods are called (if present) when the garbage collector collects an object of this type (if
$destroy is set to a true value). This comes with a performance overhead, so should only be set to a true value if necessary.
method compose_type(Mu , )
$type (that is, finalizes it to be ready for instantiation). See https://github.com/perl6/nqp/blob/master/docs/6model/repr-compose-protocol.markdown for what
$configuration can contain (until we have better docs, sorry).
method rebless(Mu , Mu )
$obj to be of type
$type. This only works if
$type type-checks against the current type of
$obj, and if the storage of
$object is a subset of that of
method is_type(Mu \obj, Mu \type --> Bool)