BrowserREPL-button-tip-on BrowserREPL-button-tip-off

EditButtonTip 2024-05-19

class Metamodel::DefiniteHOW

Metaobject for type definiteness

    class Metamodel::DefiniteHOW
        does Metamodel::Documenting
            { }
Code

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

Type objects may be given a type smiley, which is a suffix that denotes their definiteness:

say Any:D.^name; # OUTPUT: «Any:D␤»
say Any:U.^name; # OUTPUT: «Any:U␤»
say Any:_.^name; # OUTPUT: «Any␤»
Code

Despite sharing a type with Any, Any:U and Any:D in particular have different type-checking behaviors from it:

say Any ~~ Any:D;     # OUTPUT: «False␤»
say Any ~~ Any:U;     # OUTPUT: «True␤»
say Any ~~ Any:_;     # OUTPUT: «True␤»
say Any.new ~~ Any:D; # OUTPUT: «True␤»
say Any.new ~~ Any:U; # OUTPUT: «False␤»
say Any.new ~~ Any:_; # OUTPUT: «True␤»
Code

This happens because Any:D and Any:U are not created with Metamodel::ClassHOW like you might expect Any type objects to be, but Metamodel::DefiniteHOW instead. This HOW defines the behavior for definite type objects such as these.

The following type declaration:

my Any constant Definite = Any:D;
Code

Is roughly equivalent to this code using the methods of Metamodel::DefiniteHOW:

my Any constant Definite = Metamodel::DefiniteHOW.new_type: base_type => Any, definite => 1;
Code

Methods§

method new_type§

method new_type(:$base_type!, :$definite!)
Code

Creates a new definite type given a base type and definiteness. $definite should either be 1 for :D types or 0 for :U types.

method name§

method name($definite_type)
Code

Returns the name of a definite type.

method shortname§

method shortname($definite_type)
Code

Returns the shortname of a definite type.

method base_type§

method base_type($definite_type)
Code

Returns the base type for a definite type:

say Any:D.^base_type.^name; # OUTPUT: «Any␤»
Code

method definite§

method definite($definite_type)
Code

Returns 1 if the definite type given is a :D type or 0 if it is a :U type.

method nominalize§

method nominalize($obj)
Code

Produces a nominal type object for a definite type. This is its base type, which may also get nominalized if it has the nominalizable archetype.

method find_method§

method find_method($definite_type, $name)
Code

Looks up a method on the base type of a definite type.

method type_check§

method type_check($definite_type, $checkee)
Code

Performs a type-check of a definite type against $checkee. This will check if $checkee is of its base type, returning True if they match or False otherwise. This metamethod can get called when a definite type is on the left-hand side of a smartmatch, for instance.

method accepts_type§

method accepts_type($definite_type, $checkee)
Code

Performs a type-check of $checkee against a definite type. This will check if $checkee is of its base type and matches its definiteness, returning True if they match or False otherwise. This metamethod can get called when the definite type is on the right-hand side of a smartmatch, for instance.