Skip to content

Commit a9c49de

Browse files
committed
Add Cool coercers ref #3361
1 parent 4e2a82b commit a9c49de

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

doc/Type/Cool.pod6

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ what type they coerce to:
112112
chomp Str
113113
chop Str
114114
codes Str
115+
Complex Numeric
116+
FatRat Numeric
117+
Int Numeric
118+
Num Numeric
119+
Rat Numeric
120+
Real Numeric
121+
UInt Numeric
115122
116123
=end table
117124
@@ -1494,6 +1501,119 @@ string|/routine/sprintf#Directives>, to format and print the arguments
14941501
=for code
14951502
"%.8f".printf(now - now ); # OUTPUT: «-0.00004118»
14961503
1504+
=head2 method Complex
1505+
1506+
Defined as:
1507+
1508+
multi method Complex()
1509+
1510+
Coerces the invocant to a L«C<Numeric>|/type/Numeric» and calls its
1511+
L«C<.Complex>|/routine/Complex» method. L<Fails|/routine/fail> if the
1512+
coercion to a C<Numeric> cannot be done.
1513+
1514+
say 1+0i.Complex; # OUTPUT: «1+0i␤»
1515+
say 2e1.Complex; # OUTPUT: «20+0i␤»
1516+
say 1.3.Complex; # OUTPUT: «1.3+0i␤»
1517+
say (-4/3).Complex; # OUTPUT: «-1.3333333333333333+0i␤»
1518+
say "foo".Complex.^name; # OUTPUT: «Failure␤»
1519+
say 1+1i.Complex; # OUTPUT: «1+1i␤»
1520+
1521+
=head2 method FatRat
1522+
1523+
Defined as:
1524+
1525+
multi method FatRat()
1526+
1527+
Coerces the invocant to a L«C<Numeric>|/type/Numeric» and calls its
1528+
L«C<.FatRat>|/routine/FatRat» method. L<Fails|/routine/fail> if the
1529+
coercion to a C<Numeric> cannot be done.
1530+
1531+
say 1+0i.Rat; # OUTPUT: «1␤»
1532+
say 2e1.Rat; # OUTPUT: «20␤»
1533+
say 1.3.Rat; # OUTPUT: «1.3␤»
1534+
say (-4/3).Rat; # OUTPUT: «-1.333333␤»
1535+
say "foo".Rat.^name; # OUTPUT: «Failure␤»
1536+
1537+
=head2 method Int
1538+
1539+
Defined as:
1540+
1541+
multi method Int()
1542+
1543+
Coerces the invocant to a L«C<Numeric>|/type/Numeric» and calls its
1544+
L«C<.Int>|/routine/Int» method. L<Fails|/routine/fail> if the
1545+
coercion to a C<Numeric> cannot be done.
1546+
1547+
say 1+0i.Int; # OUTPUT: «1␤»
1548+
say 2e1.Int; # OUTPUT: «20␤»
1549+
say 1.3.Int; # OUTPUT: «1␤»
1550+
say (-4/3).Int; # OUTPUT: «-1␤»
1551+
say "foo".Int.^name; # OUTPUT: «Failure␤»
1552+
1553+
=head2 method Num
1554+
1555+
Defined as:
1556+
1557+
multi method Num()
1558+
1559+
Coerces the invocant to a L«C<Numeric>|/type/Numeric» and calls its
1560+
L«C<.Num>|/routine/Num» method. L<Fails|/routine/fail> if the
1561+
coercion to a C<Numeric> cannot be done.
1562+
1563+
say 1+0i.Num; # OUTPUT: «1␤»
1564+
say 2e1.Num; # OUTPUT: «20␤»
1565+
say 1.3.Num; # OUTPUT: «1.3␤»
1566+
say (-4/3).Num; # OUTPUT: «-1.3333333333333333␤»
1567+
say "foo".Num.^name; # OUTPUT: «Failure␤»
1568+
1569+
=head2 method Rat
1570+
1571+
Defined as:
1572+
1573+
multi method Rat()
1574+
1575+
Coerces the invocant to a L«C<Numeric>|/type/Numeric» and calls its
1576+
L«C<.Rat>|/routine/Rat» method. L<Fails|/routine/fail> if the
1577+
coercion to a C<Numeric> cannot be done.
1578+
1579+
say 1+0i.Rat; # OUTPUT: «1␤»
1580+
say 2e1.Rat; # OUTPUT: «20␤»
1581+
say 1.3.Rat; # OUTPUT: «1.3␤»
1582+
say (-4/3).Rat; # OUTPUT: «-1.333333␤»
1583+
say "foo".Rat.^name; # OUTPUT: «Failure␤»
1584+
1585+
=head2 method Real
1586+
1587+
Defined as:
1588+
1589+
multi method Real()
1590+
1591+
Coerces the invocant to a L«C<Numeric>|/type/Numeric» and calls its
1592+
L«C<.Real>|/routine/Real» method. L<Fails|/routine/fail> if the
1593+
coercion to a C<Numeric> cannot be done.
1594+
1595+
say 1+0i.Real; # OUTPUT: «1␤»
1596+
say 2e1.Real; # OUTPUT: «20␤»
1597+
say 1.3.Real; # OUTPUT: «1.3␤»
1598+
say (-4/3).Real; # OUTPUT: «-1.333333␤»
1599+
say "foo".Real.^name; # OUTPUT: «Failure␤»
1600+
1601+
=head2 method UInt
1602+
1603+
Defined as:
1604+
1605+
multi method UInt()
1606+
1607+
Coerces the invocant to an L«C<Int>|/type/Int». L<Fails|/routine/fail>
1608+
if the coercion to an C<Int> cannot be done or if the C<Int> the invocant
1609+
had been coerced to is negative.
1610+
1611+
say 1+0i.UInt; # OUTPUT: «1␤»
1612+
say 2e1.UInt; # OUTPUT: «20␤»
1613+
say 1.3.UInt; # OUTPUT: «1␤»
1614+
say (-4/3).UInt.^name; # OUTPUT: «Failure␤»
1615+
say "foo".UInt.^name; # OUTPUT: «Failure␤»
1616+
14971617
=end pod
14981618

14991619
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)