@@ -228,7 +228,8 @@ the value part of the L<colon-pair|/type/Pair>.
228
228
X < |type constraint,:D >
229
229
X < |type constraint,:U >
230
230
X < |type constraint,:_ >
231
- = head3 Constraining defined and undefined values
231
+
232
+ = head3 Constraining argument definiteness
232
233
233
234
Normally, a type constraint only checks whether the value of the parameter is of
234
235
the correct type. Crucially, both I < object instances > and I < type objects > will
@@ -259,7 +260,7 @@ and type objects (C<Int>). Consider the following code:
259
260
Here we really only want to deal with string instances, not type objects. To do
260
261
this, we can use the C < :D > type constraint. This constraint checks that the
261
262
value passed is an I < object instance > , in a similar fashion to calling its
262
- L < DEFINITE|/language/mop#DEFINITE > method.
263
+ L < DEFINITE|/language/mop#DEFINITE > (meta) method.
263
264
264
265
To warm up, let's apply C < :D > to the right-hand side of our humble C < Int > example:
265
266
@@ -387,6 +388,34 @@ In 6.d language, the default default is the type object without the smiley const
387
388
use v6.d.PREVIEW;
388
389
my Int:D $x .= new: 42; # OUTPUT: «42»
389
390
391
+ A closing remark on terminology: this section is about the use of the type
392
+ smileys C < :D > and C < :U > to constrain the definiteness of arguments.
393
+ Occasionally I < definedness > is used as a synonym for I < definiteness > ; this may
394
+ be confusing, since the terms have subtly different meanings.
395
+
396
+ As explained above, I < definiteness > is concerned with the distinction between
397
+ type objects and object instances. A type object is always indefinite, while an
398
+ object instance is always definite. Whether or not an object is a type
399
+ object/indefinite or an object instance/definite can be verified using the
400
+ L < DEFINITE|/language/mop#DEFINITE > (meta)method.
401
+
402
+ I < Definiteness > should be distinghuished from I < definedness > , which is concerned
403
+ with the difference between defined and undefined objects. Whether an object is
404
+ defined or undefined can be verified using the C < .defined > -method, which is
405
+ implemented in class L < Mu|/type/Mu > . By default a type object is considered
406
+ undefined, while an object instance is considered defined; that is: C < .defined >
407
+ returns C < False > on a type object, and C < True > otherwise. But this default
408
+ behaviour may be overridden by subclasses. An example of a subclass that
409
+ overrides the default C < .defined > behaviour is L < Failure|type/Failure > ,
410
+ so that even an instantiated C < Failure > acts as an undefined value:
411
+
412
+ my $a = Failure; # Initialize with type object
413
+ my $b = Failure.new("foo"); # Initialize with object instance
414
+ say $a.DEFINITE; # Output: «False» : indefinite type object
415
+ say $b.DEFINITE; # Output: «True» : definite object instance
416
+ say $a.defined; # Output: «False» : default response
417
+ say $b.defined; # Output: «False» : .defined override
418
+
390
419
= head3 Constraining signatures of C < Callable > s
391
420
392
421
The signature of a L < Callable > parameter can be constrained by
0 commit comments