HaloO,
Moritz Lenz wrote:
When you read carefully through S29 you'll notice that most methods in
immutable classes (like Str, List, Int) only return modified copies,
even if they mutate the string in Perl 5.
Great!
(There are some exceptions like Str.substr, which explicitly 'is rw',
and which implies that the object somehow has to gain access to its
container).
Reading the description there I wonder how this is supposed to work.
our Str multi method substr (Str $string: StrPos $start, Int
$length)
is rw is export
$string ~~ /(barney)/;
substr($string, $0.from, $0.to) = "fred";
The 'is rw' is on the method but I guess it is foreseen that the
result is stored in $string without preserving the identity of the
string?
my $a = "His name is barney";
my $b = $a;
$a ~~ /(barney)/;
substr($a, $0.from, $0.to) = "fred";
say $b; # prints fred?
If substr should get access to the container passed as argument then
the signature should read
our Str multi method substr (Str $string is rw: StrPos $start,
Int $length) is rw is export
and the docu should say that the result is written into the container
passed in the invocant slot. But the 'is rw' on the invocant has the
drawback that calling with a string literal is precluded.
Regards, TSa.
--
"The unavoidable price of reliability is simplicity" -- C.A.R. Hoare
"Simplicity does not precede complexity, but follows it." -- A.J. Perlis
1 + 2 + 3 + 4 + ... = -1/12 -- Srinivasa Ramanujan