Skip to content

Commit 35e20e2

Browse files
committed
define coercion types, value/impl types better
We were using two different meanings of "value type", so now value vs implementation type is "of" type vs "container" type. We also now have a coercive type syntax, so there's no longer any need for a separate 'as' trait for subs.
1 parent 5f35620 commit 35e20e2

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

S02-bits.pod

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Synopsis 2: Bits and Pieces
1313

1414
Created: 10 Aug 2004
1515

16-
Last Modified: 2 Aug 2011
17-
Version: 236
16+
Last Modified: 8 Aug 2011
17+
Version: 237
1818

1919
This document summarizes Apocalypse 2, which covers small-scale
2020
lexical items and typological issues. (These Synopses also contain
@@ -515,6 +515,25 @@ You can pass in arguments to the constructor as well:
515515
my Dog $cerberus .= new(heads => 3);
516516
my Dog $cerberus = $cerberus.new(heads => 3); # same thing
517517

518+
=head2 Coercive type declarations
519+
520+
The parenthesized form of type coercion may be used in declarations
521+
where it makes sense to accept a wider set of types but coerce them
522+
to a narrow type. (This only works for one-way coercion, so you
523+
may not declare any C<rw> parameter with a coercive type.) The type
524+
outside the parens indicates the desired end result, and subsequent
525+
code may depend on the it being that type. The type inside the parens
526+
indicates the acceptable set of types that are allowed to be bound or
527+
assigned to this location via coercion. If the wide type is omitted,
528+
C<Any> is assumed. In any case, the wide type is only indicative of
529+
permission to coerce; there must still be an available coercion routine
530+
from the wide type to the narrow type to actually perform the coercion.
531+
532+
sub foo (Str(Any) $y) {...}
533+
sub foo (Str() $y) {...} # same thing
534+
535+
my Num(Cool) $x = prompt "Gimme a number";
536+
518537
=head2 Containers of Native Types
519538

520539
If you say
@@ -1545,60 +1564,57 @@ a notable exception.
15451564

15461565
See L<S06/"Wrapping"> for a discussion of soft vs. hard routines.
15471566

1548-
=head2 Value types
1567+
=head2 Of types
15491568

15501569
Explicit types are optional. Perl variables have two associated types:
1551-
their "value type" and their "implementation type". (More generally, any
1552-
container has an implementation type, including subroutines and modules.)
1553-
The value type is stored as its C<of> property, while the implementation
1570+
their "of type" and their "container type". (More generally, any
1571+
container has a container type, including subroutines and modules.)
1572+
The of type is stored as its C<of> property, while the container
15541573
type of the container is just the object type of the container itself.
15551574
The word C<returns> is allowed as an alias for C<of>.
15561575

1557-
The value type specifies what kinds of values may be stored in the
1558-
variable. A value type is given as a prefix or with the C<of> keyword:
1576+
The of type specifies what kinds of values may be stored in the
1577+
variable. An of type is given as a prefix or with the C<of> keyword:
15591578

15601579
my Dog $spot;
15611580
my $spot of Dog;
15621581

15631582
In either case this sets the C<of> property of the container to C<Dog>.
15641583

1565-
Subroutines have a variant of the C<of> property, C<as>, that sets
1566-
the C<as> property instead. The C<as> property specifies a
1567-
constraint (or perhaps coercion) to be enforced on the return value (either
1568-
by explicit call to C<return> or by implicit fall-off-the-end return).
1569-
This constraint, unlike the C<of> property, is not advertised as the
1570-
type of the routine. You can think of it as the implicit type signature of
1571-
the (possibly implicit) return statement. It's therefore available for
1572-
type inferencing within the routine but not outside it. If no C<as> type
1573-
is declared, it is assumed to be the same as the C<of> type, if declared.
1574-
1575-
sub get_pet() of Animal {...} # of type, obviously
1576-
sub get_pet() returns Animal {...} # of type
1577-
our Animal sub get_pet() {...} # of type
1578-
sub get_pet() as Animal {...} # as type
1579-
1580-
A value type on an array or hash specifies the type stored by each element:
1584+
An of type on an array or hash specifies the type stored by each element:
15811585

15821586
my Dog @pound; # each element of the array stores a Dog
15831587

15841588
my Rat %ship; # the value of each entry stores a Rat
15851589

15861590
The key type of a hash may be specified as a shape trait--see S09.
15871591

1588-
=head2 Implementation types
1592+
Containers enforce type safety on setting, whereas subroutines enforce type
1593+
safety on return. The C<returns> declarations is an alias for the C<of> type
1594+
of a subroutine.
1595+
1596+
sub get_pet() of Animal {...} # of type, obviously
1597+
sub get_pet() returns Animal {...} # of type
1598+
our Animal sub get_pet() {...} # of type
1599+
1600+
To coerce your return value, use a coercion type:
1601+
1602+
sub get_pet() returns Pet(Animal) {...} # coerce any Animal to Pet
1603+
1604+
=head2 Container types
15891605

1590-
The implementation type specifies how the variable itself is implemented. It is
1606+
The container type specifies how the variable itself is implemented. It is
15911607
given as a trait of the variable:
15921608

15931609
my $spot is Scalar; # this is the default
15941610
my $spot is PersistentScalar;
15951611
my $spot is DataBase;
15961612

1597-
Defining an implementation type is the Perl 6 equivalent to tying
1613+
Defining a container type is the Perl 6 equivalent to tying
15981614
a variable in Perl 5. But Perl 6 variables are tied directly at
15991615
declaration time, and for performance reasons may not be tied with a
16001616
run-time C<tie> statement unless the variable is explicitly declared
1601-
with an implementation type that does the C<Tieable> role.
1617+
with a container type that does the C<Tieable> role.
16021618

16031619
However, package variables are always considered C<Tieable> by default.
16041620
As a consequence, all named packages are also C<Tieable> by default.

0 commit comments

Comments
 (0)