/
NumStr.pod
71 lines (44 loc) 路 1.84 KB
/
NumStr.pod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
=begin pod
=TITLE class NumStr
=SUBTITLE Dual Value Floating-point number and String
class NumStr is Num is Str
The dual value types (often referred to as L<allomorphs|/language/glossary#Allomorph>)
allow for the representation of a value as both a string and a numeric type, typically
they will be created for you when the context is "stringy" but they can be determined
to be numbers, such as in some L<quoting constructs|/language/quoting>:
my $f = <42.1e0>; say $f.WHAT; # (NumStr)
A C<NumStr> can be used interchangeably where one might use a L<Str|/type/Str> or a
L<Num|/type/Num> :
my $f = <42.1e0>;
my $i = $f + 0.1e0;
my $s = $f ~ " and a bit more";
=head1 Methods
=head2 method new
method new(Num $i, Str $s)
The constructor requires both the C<Num> and the C<Str> value, when constructing one
directly the values can be whatever is required:
my $f = NumStr.new(42.1e0, "forty two and a bit");
say +$f; # -> 42.1
say ~$f; # -> "forty two and a bit"
=head2 method Numeric
method Numeric
The numeric coercion is applied when the C<NumStr> is used in a numeric context,
such as a numeric comparison or smart match against a numeric value. It will return
the C<Num> value.
=head2 method Num
method Num
Returns the C<Num> value of the C<NumStr>.
=head2 method Str
Returns the string value of the C<NumStr>.
=head1 Operators
=head2 infix cmp
multi sub infix:<cmp>(NumStr:D $a, NumStr:D $b)
Compare two C<NumStr> objects. The comparison is done on the C<Num> value first and
then on the C<Str> value. If you want to compare in a different order then you would
coerce to a C<Num> or C<Str> value first:
my $f = NumStr.new(42.1e0, "smaller");
my $g = NumStr.new(43.1e0, "larger");
say $f cmp $g; # Less
say $f.Str cmp $g.Str; # More
=end pod
# vim: expandtab shiftwidth=4 ft=perl6