Skip to content

Commit b1b494d

Browse files
committed
[CaR Grant] Document MidRatStr
1 parent ccfafec commit b1b494d

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

doc/Type/MidRatStr.pod6

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
=begin pod
2+
3+
=TITLE class MidRatStr
4+
5+
=SUBTITLE Dual Value MidRat number and String
6+
7+
class MidRatStr is MidRat is Str {}
8+
9+
The dual value types (often referred to as L<allomorphs|/language/glossary#Allomorph>)
10+
allow for the representation of a value as both a string and a numeric type, typically
11+
they will be created for you when the context is "stringy" but they can be determined
12+
to be numbers, such as in some L<quoting constructs|/language/quoting>:
13+
14+
my $f = <0.00000000000000000001>; say $f.^name; # OUTPUT: «MidRatStr␤»
15+
16+
As a subclass of both L<MidRat> and L<Str>, a C<MidRatStr>
17+
will be accepted where either is expected. However, C<MidRatStr> does not share
18+
object identity with C<Rat>- or C<Str>-only variants:
19+
20+
my $midrat-str = <0.00000000000000000001>;
21+
my Rat $rat = $midrat-str; # OK!
22+
my Str $str = $midrat-str; # OK!
23+
say 0.00000000000000000001 ∈ <0.00000000000000000001 55 1>;
24+
# False; ∈ operator cares about object identity
25+
26+
=head1 Methods
27+
28+
=head2 method new
29+
30+
method new(MidRat $i, Str $s)
31+
32+
The constructor requires both the C<MidRat> and the C<Str> value,
33+
when constructing one directly the values can be whatever is required:
34+
35+
my $f = MidRatStr.new(<0.00000000000000000001>, "zeros and one");
36+
say +$f; # OUTPUT: «0.00000000000000000001␤»
37+
say ~$f; # OUTPUT: «"zeros and one"␤»
38+
39+
=head2 method Bool
40+
41+
Defined as:
42+
43+
multi method Bool(MidRatStr:D: --> Bool:D)
44+
45+
I<This method may be provided by the parent classes and not implemented in MidRatStr directly>.
46+
47+
Returns C<False> if the L<numerator> of the numeric portion is C<0>, otherwise returns C<True>.
48+
This applies for zero-denominator L<MidRatStr> as well, despite C<.Num.Bool>
49+
of it being C<True>. String portion is not considered.
50+
51+
=head2 method Capture
52+
53+
Defined as:
54+
55+
method Capture(MidRatStr:D --> Capture:D)
56+
57+
Equivalent to L«C<Mu.Capture>|/type/Mu#method_Capture».
58+
59+
=head2 method MidRat
60+
61+
method MidRat
62+
63+
Returns the C<MidRat> portion of the C<MidRatStr>.
64+
65+
=head2 method Numeric
66+
67+
Defined as:
68+
69+
multi method Numeric(MidRatStr:D: --> MidRat:D)
70+
multi method Numeric(MidRatStr:U: --> MidRat:D)
71+
72+
The C<:D> variant returns the numeric portion of the invocant. The C<:U> variant issues
73+
a warning about using an uninitialized value in numeric context and then returns a L<MidRat> object numerically equivalent to zero.
74+
75+
=head2 method Real
76+
77+
Defined as:
78+
79+
multi method Real(Real:D: --> MidRat:D)
80+
multi method Real(Real:U: --> MidRat:D)
81+
82+
The C<:D> variant returns the numeric portion of the invocant. The C<:U> variant issues
83+
a warning about using an uninitialized value in numeric context and then returns a L<MidRat> object numerically equivalent to zero.
84+
85+
=head2 method Str
86+
87+
Returns the string value of the C<MidRatStr>.
88+
89+
=head2 method ACCEPTS
90+
91+
Defined as:
92+
93+
multi method ACCEPTS(MidRatStr:D: Any:D $value)
94+
95+
If C<$value> is L<Numeric> (including another
96+
L<allomorph|/language/glossary#index-entry-Allomorph>), checks if invocant's
97+
L<Numeric> part L<ACCEPTS|/type/Numeric#method_ACCEPTS> the C<$value>. If
98+
C<$value> is L<Str>, checks if invocant's L<Str> part
99+
L<ACCEPTS|/type/Str#method_ACCEPTS> the C<$value>. If value is anything else,
100+
checks if both L<Numeric> and L<Str> parts C<ACCEPTS> the C<$value>.
101+
102+
my $mid-rat := <0.00000000000000000001>;
103+
say $mid-rat ~~ ".00000000000000000001"; # OUTPUT: «False␤»
104+
say $mid-rat ~~ .00000000000000000001 ; # OUTPUT: «True␤»
105+
say $mid-rat ~~ <.00000000000000000001>; # OUTPUT: «True␤»
106+
107+
=head1 Operators
108+
109+
=head2 infix cmp
110+
111+
multi sub infix:<cmp>(MidRatStr:D $a, MidRatStr:D $b)
112+
113+
Compare two C<MidRatStr> objects. The comparison is done on the C<MidRat> value
114+
first and then on the C<Str> value. If you want to compare in a different order
115+
then you would coerce to the C<MidRat> or C<Str> values first:
116+
117+
my $f = MidRatStr.new(MidRat.new(421,1000), "smaller");
118+
my $g = MidRatStr.new(MidRat.new(431,1000), "larger");
119+
say $f cmp $g; # OUTPUT: «Less␤»
120+
say $f.Str cmp $g.Str; # OUTPUT: «More␤»
121+
122+
=end pod
123+
124+
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)