@@ -204,7 +204,7 @@ part of the L<colon-pair|/type/Pair>.
204
204
= head3 X < Constraining Defined and Undefined Values|
205
205
type constraint,:D;type constraint,:U;type constraint,:_>
206
206
207
- Normally, a type constraint only checks whether the value passed is of the
207
+ Normally, a type constraint only checks whether the value of the parameter is of the
208
208
correct type.
209
209
210
210
sub limit-lines(Str $s, Int $limit) {
@@ -220,9 +220,9 @@ correct type.
220
220
# (Str:D $: *%_)»
221
221
say limit-lines "a \n b", Int # Always returns the max number of lines
222
222
223
- In the code above, we really only want to deal with defined strings. To
224
- do this, we use the C < :D > type constraint. This constraint checks the
225
- value passed is an I < object instance > , in a similar fashion to calling
223
+ In the code above, we really only want to deal with string instances, not
224
+ type objects. To do this, we use the C < :D > type constraint. This constraint
225
+ checks the value passed is an I < object instance > , in a similar fashion to calling
226
226
the C < .DEFINITE > method on the value:
227
227
228
228
sub limit-lines(Str:D $s, Int $limit) { };
@@ -234,7 +234,7 @@ the C<.DEFINITE> method on the value:
234
234
This is much better than the way the program failed before, since here the
235
235
reason for failure is clearer.
236
236
237
- It's also possible that undefined types are the only ones that make
237
+ It's also possible that type objects are the only ones that make
238
238
sense for a routine to accept. This can be done with the C < :U > type
239
239
constraint, which checks the value passed if it is a I < type object > ,
240
240
rather than an object instance. For example, we can turn the
@@ -276,6 +276,17 @@ The L<Classes and Objects|/language/classtut#Starting_with_class>
276
276
document further elaborates on the concepts of instances and type
277
277
objects and discovering them with the C < .DEFINITE > method.
278
278
279
+ Keep in mind all parameters have values; even optional ones have
280
+ default defaults that are the type object of the constrained type
281
+ for explicit type constraints or an L < Any > type object if no
282
+ explicit type constraint exists. This means that if you use the
283
+ C < :D > type smiley, you'd need to provide a default value or make
284
+ the parameter required. Otherwise, the default default would be
285
+ a type object, which would fail the definiteness constraint.
286
+
287
+ sub divide (Int:D :$a = 2, Int:D :$b!) { say $a/$b }
288
+ divide :1a, :2b; # OUTPUT: «0.5»
289
+
279
290
= head3 X « Constraining signatures of Callables|Callable (constrain) »
280
291
281
292
A L < Callable > parameter can be constrained by its signature, by specifying
0 commit comments