Skip to content

Commit f5f3265

Browse files
authored
Merge pull request #3318 from Kaiepi/metamodel-definitehow
Document Metamodel::DefiniteHOW
2 parents ae55d38 + 90f8d3b commit f5f3265

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

doc/Type/Metamodel/DefiniteHOW.pod6

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

type-graph.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Metamodel::ConcreteRoleHOW does Metamodel::Naming does Metamodel::Version
1111
class Metamodel::ContainerDescriptor
1212
class Metamodel::CurriedRoleHOW does Metamodel::RolePunning does Metamodel::TypePretense
1313
role Metamodel::DefaultParent
14+
class Metamodel::DefiniteHOW does Metamodel::Documenting
1415
class Metamodel::BaseDispatcher
1516
class Metamodel::MethodDispatcher is Metamodel::BaseDispatcher
1617
class Metamodel::MultiDispatcher is Metamodel::BaseDispatcher

xt/code.pws

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ dddddeff
173173
ddthh
174174
debugtype
175175
defg
176+
definitehow
176177
dependencyspecification
177178
desc
178179
dest

xt/words.pws

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ chainable
174174
charset
175175
charsorbytes
176176
chdir
177+
checkee
177178
cheatsheet
178179
chmod
179180
chown
@@ -805,6 +806,11 @@ nohighlight
805806
nok
806807
nom
807808
nomatch
809+
nominalizable
810+
nominalize
811+
nominalizes
812+
nominalized
813+
nominalizing
808814
nonchaining
809815
nonintuitive
810816
nonspacing

0 commit comments

Comments
 (0)