|
| 1 | +=begin pod :kind("Type") :subkind("class") :category("metamodel") |
| 2 | +
|
| 3 | +=TITLE class Metamodel::DefiniteHOW |
| 4 | +
|
| 5 | +=SUBTITLE Metaobject for type definiteness |
| 6 | +
|
| 7 | + class Metamodel::DefiniteHOW |
| 8 | + does Metamodel::Documenting |
| 9 | + { } |
| 10 | +
|
| 11 | +I<Warning>: this class is part of the Rakudo implementation, and is not |
| 12 | +a part of the language specification. |
| 13 | +
|
| 14 | +Type objects may be given a I<type smiley>, which is a suffix that |
| 15 | +denotes their definiteness: |
| 16 | +
|
| 17 | +=begin code |
| 18 | +say Any:D.^name; # OUTPUT: «Any:D» |
| 19 | +say Any:U.^name; # OUTPUT: «Any:U» |
| 20 | +say Any:_.^name; # OUTPUT: «Any» |
| 21 | +=end code |
| 22 | +
|
| 23 | +Despite sharing a type with C<Any>, C<Any:U> and C<Any:D> in particular |
| 24 | +have different type-checking behaviors from it: |
| 25 | +
|
| 26 | +=begin code |
| 27 | +say Any ~~ Any:D; # OUTPUT: «False» |
| 28 | +say Any ~~ Any:U; # OUTPUT: «True» |
| 29 | +say Any ~~ Any:_; # OUTPUT: «False» |
| 30 | +say Any.new ~~ Any:D; # OUTPUT: «True» |
| 31 | +say Any.new ~~ Any:U; # OUTPUT: «False» |
| 32 | +say Any.new ~~ Any:_; # OUTPUT: «True» |
| 33 | +=end code |
| 34 | +
|
| 35 | +This happens because C<Any:D> and C<Any:U> are not created with |
| 36 | +L<Metamodel::ClassHOW|/type/Metamodel::ClassHOW> like you might expect |
| 37 | +C<Any> type objects to be, but C<Metamodel::DefiniteHOW> instead. This |
| 38 | +HOW defines the behavior for definite type objects such as these. |
| 39 | +
|
| 40 | +The following type declaration: |
| 41 | +
|
| 42 | +=for code |
| 43 | +my Any constant Definite = Any:D; |
| 44 | +
|
| 45 | +Is roughly equivalent to this code using the methods of |
| 46 | +C<Metamodel::DefiniteHOW>: |
| 47 | +
|
| 48 | +=for code |
| 49 | +my Any constant Definite = Metamodel::DefiniteHOW.new_type: base_type => Any, definite => 1; |
| 50 | +
|
| 51 | +=head1 Methods |
| 52 | +
|
| 53 | +=head2 method new_type |
| 54 | +
|
| 55 | + method new_type(:$base_type!, :$definite!) |
| 56 | +
|
| 57 | +Creates a new definite type given a base type and definiteness. |
| 58 | +C<$definite> should either be C<1> for C<:D> types or C<0> for C<:U> |
| 59 | +types. |
| 60 | +
|
| 61 | +=head2 method name |
| 62 | +
|
| 63 | + method name($definite_type) |
| 64 | +
|
| 65 | +Returns the name of a definite type. |
| 66 | +
|
| 67 | +=head2 method shortname |
| 68 | +
|
| 69 | + method shortname($definite_type) |
| 70 | +
|
| 71 | +Returns the shortname of a definite type. |
| 72 | +
|
| 73 | +=head2 method base_type |
| 74 | +
|
| 75 | + method base_type($definite_type) |
| 76 | +
|
| 77 | +Returns the base type for a definite type: |
| 78 | +
|
| 79 | +=for code |
| 80 | +say Any:D.^base_type.^name; # OUTPUT: «Any» |
| 81 | +
|
| 82 | +=head2 method definite |
| 83 | +
|
| 84 | + method definite($definite_type) |
| 85 | +
|
| 86 | +Returns C<1> if the definite type given is a C<:D> type or C<0> if it is |
| 87 | +a C<:U> type. |
| 88 | +
|
| 89 | +=head2 method nominalize |
| 90 | +
|
| 91 | + method nominalize($obj) |
| 92 | +
|
| 93 | +Produces a nominal type object for a definite type. This is its base |
| 94 | +type, which may also get nominalized if it has the C<nominalizable> |
| 95 | +archetype. |
| 96 | +
|
| 97 | +=head2 method find_method |
| 98 | +
|
| 99 | + method find_method($definite_type, $name) |
| 100 | +
|
| 101 | +Looks up a method on the base type of a definite type. |
| 102 | +
|
| 103 | +=head2 method type_check |
| 104 | +
|
| 105 | + method type_check($definite_type, $checkee) |
| 106 | +
|
| 107 | +Performs a type-check of a definite type against C<$checkee>. This will |
| 108 | +check if C<$checkee> is of its base type, returning C<True> if they |
| 109 | +match or C<False> otherwise. This metamethod can get called when a |
| 110 | +definite type is on the left-hand side of a smartmatch, for instance. |
| 111 | +
|
| 112 | +=head2 method accepts_type |
| 113 | +
|
| 114 | + method accepts_type($definite_type, $checkee) |
| 115 | +
|
| 116 | +Performs a type-check of C<$checkee> against a definite type. This will |
| 117 | +check if C<$checkee> is of its base type and matches its definiteness, |
| 118 | +returning C<True> if they match or C<False> otherwise. This metamethod |
| 119 | +can get called when the definite type is on the right-hand side of a |
| 120 | +smartmatch, for instance. |
| 121 | +
|
| 122 | +=end pod |
0 commit comments