develooper Front page | perl.perl5.porters | Postings from June 2019

Re: Making join() respect string-concat operator

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
June 5, 2019 09:30
Subject:
Re: Making join() respect string-concat operator
Message ID:
20190605093030.GJ4761@iabyn.com
On Tue, Jun 04, 2019 at 06:36:25PM +0100, Paul "LeoNerd" Evans wrote:
> (this is simpler rehash of
>   https://www.nntp.perl.org/group/perl.perl5.porters/2010/06/msg160741.html )
> 
> I would like core's join() operator to respect the string-concat
> operator overloading of any arguments passed to it.
> 
> I.e. that the result of
> 
>   join( $sep, $x, $y, $z )
> 
> always be indistinguishable from the result of
> 
>   $x . $sep . $y . $sep . $z
> 
> even if any of $sep, $x, $y or $z has operator overloading.
> 
> In particular, if any of those operators returned an object rather than
> a plain string, then the overall join() operator should by now have
> returned that object.
> 
> As things currently stand, join() always stringifies each argument
> individually, then yields a plain string containing all the characters
> concatenated into it.


I'm not very keen on the idea. It's making join() into a very special
case when it comes to overloading. If we were to go down the path of
making perl  builtin functions overloadable, then we really ought to
add the facility to make them overloaded (cf the mathematical functions
sin cos etc, which are already overloadable).

Not that I'm terribly keen on that either.

Your proposal would change how often $sep is evaluated. At the moment
if it is magical, its magic is called once. Then if the result of the
magic call is overloaded, the '""' method is called once.

If the join is treated as
    $x . $sep . $y . $sep . $z
then the magic on $sep would be called multiple times, each time
potentially returning a different value.

Also, if we're going down the path of "find places where perl concatenates
strings and if so call the overload concatenation method", then where do
we stop? Do we support overloading in Perl_sv_catsv() and friends?

At the moment its reasonably clear that where you explicitly use the '.'
operator (and double-quoted strings, which are syntactic sugar for
concatenation) you should expect concat overloading to be honoured.

We should keep a clear distinction between:

    concat operator '.' appears in the source code

and

    perl concatenates two strings for some reason.

Only the first should support concat overloading.


-- 
Fire extinguisher (n) a device for holding open fire doors.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About