Skip to content

Commit 94c2270

Browse files
committed
Fix up info on Type Constraints
Closes #1870 D#1870 Closes #1871 D#1871 Based on https://irclog.perlgeek.de/perl6/2018-03-26#i_15968371
1 parent 7b4a002 commit 94c2270

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

doc/Language/containers.pod6

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,31 @@ last resort, you can use Promises to L<handle|/type/Promise#method_in> timeouts.
324324
Any container can have a type constraint in the form of
325325
a L<type object|/language/typesystem#Type_objects> or a L<subset|/language/typesystem#subset>.
326326
Both can be placed between a declarator and the variable name or after the trait L<of|/type/Variable#trait_is_dynamic>.
327-
The constraint is a property of the container, not the variable. Any (re-)binding may change the
328-
type constraint or remove the constraint altogether if bound to a value instead of a container.
329-
Introspection of type constraints on containers is provided by C<.VAR.of>.
330-
331-
EVAL ‚my Int $i = 42; $i := "forty plus two";‘;
332-
CATCH { default { say .^name, ' ', .Str } }
333-
# OUTPUT: «Type check failed in binding; expected Int but got Str ("forty plus two")␤…»
334-
335-
The default type constraint of a C<Scalar> container is Any. For &-sigiled
336-
containers it's C<Callable> and @-sigiled containers are of type C<Array>. Please
337-
note that binding can change the type constraint.
327+
The constraint is a property of the container, not the variable.
328+
329+
Variables may have no container in them, yet still offer the ability to
330+
re-bind and typecheck that rebind. The reason for that is in such cases the
331+
binding operator performs the typecheck:
332+
333+
my Int \z = 42;
334+
z := 100; # OK
335+
z := "x"; # Typecheck failure
336+
337+
The same isn't the case when, say, binding to a L<Hash> key, as there the
338+
binding is handled by a method call (even though the syntax remains the same,
339+
using C<:=> operator).
340+
341+
The default type constraint of a C<Scalar> container is L<Mu>.
342+
Introspection of type constraints on containers is provided by C<.VAR.of>
343+
method, which for C<@> and C<%> sigiled variables gives the constraint for
344+
values:
345+
346+
my Str $x;
347+
say $x.VAR.of; # OUTPUT: «(Str)␤»
348+
my Num @a;
349+
say @a.VAR.of; # OUTPUT: «(Num)␤»
350+
my Int %h;
351+
say %h.VAR.of; # OUTPUT: «(Int)␤»
338352
339353
=head1 Custom containers
340354

0 commit comments

Comments
 (0)