Re: join() and split() to respect string overload operations
[email protected] (Paul LeoNerd Evans)
| Newsgroups | perl.perl5.porters |
|---|---|
| Message-ID | <[email protected]> |
On Sat, Jun 05, 2010 at 07:54:47AM +0100, Zefram wrote:
> Paul LeoNerd Evans wrote:
> >So for this, it would be nice if a split $re, $str could observe some
> >magic on the $str object and act accordingly.
>
> You're overcomplicating here.
Really? I'm not sure I agree.
Create a number-like object with well-behaved operator overloads, and
observe the following identities:
my $x = NumberLike->new(10);
$x + 1 - 1 == $x
$x * 2 / 2 == $x
And so on... I.e. all the reasonable numeric operators have inverse
operators, so people have the opportunity to create well-behaved sets
that preserve the usual identities one expects.
Now create a string-like object with well-behaved operator overload for
concat, and observe you cannot still preserve the following identities:
my $str = StringLike->new("Foo");
substr( $str . "x", 0, len $str ) eq $str
substr( "x" . $str, 0, 1 ) eq $str
substr( $str, 0, 1 ) . substr( $str, 1 ) eq $str
We allow string-like classes to overload the concat operator, to form
bigger strings out of smaller ones. Why not allow them to overload the
substr operator, to extract smaller strings back out of bigger ones?
My suggestion of allowing split() to use it is a separate issue; and
could easily be implemented, as for join(), by defining a standard
"works for plain strings or any overloads" function, which extracts
cutpoints using m//g, then maps substr over them. By using the standard
protocol that use overload substr => $METHOD can declare a substr()
overload, it could be written in plainperl, in the same way that join()
can be reimplemented using reduce concat. Without this standard
location, how is the split-alike to know how to extract substrings of
custom classes? After all, all normal operator overloading is just
syntax sugar for knowing which method to call;
$foo + $bar vs. $foo->add( $bar )
That all said, right now I only have one use case for an overloaded
substr(), being my own class; but I feel fairly sure somewhere on CPAN
there'll be another implementation of a string-like class which
overloads concat, yet can't provide a CORE::substr() magic. My current
solution in my class is to provide a ->substr() method, but obviously
the user of the string has to know to call that. It would be great if
perl provided the interface protocol, via overload, which means an
object class can declare how to do that in a standard way everyone knows
where to look.
I may have a hunt on CPAN to see if I can find any other classes which
would benefit from this; though obviously it's a little hard looking for
code which by definition doesn't exist yet :)
--
Paul "LeoNerd" Evans
[email protected]
ICQ# 4135350 | Registered Linux# 179460
http://www.leonerd.org.uk/
signature.asc
(application/pgp-signature, 190 B)
-----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) iD8DBQFMDNUxvLS2TC8cBo0RAudsAJ9cm5FOGwULYFriCnPoW3c1VukVfQCgjPnw giFY4vYMTOZLTpNTv66h1EY= =dVdi -----END PGP SIGNATURE-----