Re: join() and split() to respect string overload operations
[email protected] (Abigail)
| Newsgroups | perl.perl5.porters |
|---|---|
| Message-ID | <20100607114600.GS12847@almanda> |
On Mon, Jun 07, 2010 at 12:17:06PM +0100, Paul LeoNerd Evans wrote:
> 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?
I don't consider 'substr' to be the inverse of '.'. In particular, it
doesn't work the other way around. If substr() is overloadable, then
substr $str, $pos, $len, $repl;
is overloaded as well. How do I reserve that with '.'?
It's not that I oppose 'substr' to be overloadable - I just don't find
the "reversability" a very compelling argument. There are other overloadable
operations that aren't reverable: abs, int, bool, "", 0+, qr, x, x=, <>, -X,
~~, ${} and friends.
Abigail