@@ -13,8 +13,8 @@ Synopsis 2: Bits and Pieces
13
13
14
14
Created: 10 Aug 2004
15
15
16
- Last Modified: 2 Aug 2011
17
- Version: 236
16
+ Last Modified: 8 Aug 2011
17
+ Version: 237
18
18
19
19
This document summarizes Apocalypse 2, which covers small-scale
20
20
lexical items and typological issues. (These Synopses also contain
@@ -515,6 +515,25 @@ You can pass in arguments to the constructor as well:
515
515
my Dog $cerberus .= new(heads => 3);
516
516
my Dog $cerberus = $cerberus.new(heads => 3); # same thing
517
517
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
+
518
537
=head2 Containers of Native Types
519
538
520
539
If you say
@@ -1545,60 +1564,57 @@ a notable exception.
1545
1564
1546
1565
See L<S06/"Wrapping"> for a discussion of soft vs. hard routines.
1547
1566
1548
- =head2 Value types
1567
+ =head2 Of types
1549
1568
1550
1569
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
1554
1573
type of the container is just the object type of the container itself.
1555
1574
The word C<returns> is allowed as an alias for C<of>.
1556
1575
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:
1559
1578
1560
1579
my Dog $spot;
1561
1580
my $spot of Dog;
1562
1581
1563
1582
In either case this sets the C<of> property of the container to C<Dog>.
1564
1583
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:
1581
1585
1582
1586
my Dog @pound; # each element of the array stores a Dog
1583
1587
1584
1588
my Rat %ship; # the value of each entry stores a Rat
1585
1589
1586
1590
The key type of a hash may be specified as a shape trait--see S09.
1587
1591
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
1589
1605
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
1591
1607
given as a trait of the variable:
1592
1608
1593
1609
my $spot is Scalar; # this is the default
1594
1610
my $spot is PersistentScalar;
1595
1611
my $spot is DataBase;
1596
1612
1597
- Defining an implementation type is the Perl 6 equivalent to tying
1613
+ Defining a container type is the Perl 6 equivalent to tying
1598
1614
a variable in Perl 5. But Perl 6 variables are tied directly at
1599
1615
declaration time, and for performance reasons may not be tied with a
1600
1616
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.
1602
1618
1603
1619
However, package variables are always considered C<Tieable> by default.
1604
1620
As a consequence, all named packages are also C<Tieable> by default.
0 commit comments