Skip to content

Commit 3796780

Browse files
authored
Clarify interaction between :D and optional params
- Include examples and clarify typecheck explosion - Remove "passed", since that implies[^1] typecheck does not apply to optional params - Reword "undefined", since there's a difference between .defined and .DEFINITE [1] https://stackoverflow.com/questions/48166325/should-constraining-a-perl-6-named-parameter-to-a-definite-value-make-it-a-requi
1 parent 64cbf77 commit 3796780

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

doc/Type/Signature.pod6

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ part of the L<colon-pair|/type/Pair>.
204204
=head3 X<Constraining Defined and Undefined Values|
205205
type constraint,:D;type constraint,:U;type constraint,:_>
206206
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
208208
correct type.
209209
210210
sub limit-lines(Str $s, Int $limit) {
@@ -220,9 +220,9 @@ correct type.
220220
# (Str:D $: *%_)»
221221
say limit-lines "a \n b", Int # Always returns the max number of lines
222222
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
226226
the C<.DEFINITE> method on the value:
227227
228228
sub limit-lines(Str:D $s, Int $limit) { };
@@ -234,7 +234,7 @@ the C<.DEFINITE> method on the value:
234234
This is much better than the way the program failed before, since here the
235235
reason for failure is clearer.
236236
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
238238
sense for a routine to accept. This can be done with the C<:U> type
239239
constraint, which checks the value passed if it is a I<type object>,
240240
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>
276276
document further elaborates on the concepts of instances and type
277277
objects and discovering them with the C<.DEFINITE> method.
278278
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+
279290
=head3 X«Constraining signatures of Callables|Callable (constrain)»
280291
281292
A L<Callable> parameter can be constrained by its signature, by specifying

0 commit comments

Comments
 (0)