Mailing List Archive

Pre-RFC: Support overloaded objects in join(), substr() builtins
Hello porters,

The perl builtins like substr, join, etc. when operating on overloaded
objects will stringify them and the result will no longer be an object
of the class with overloads, even if the concat function is overloaded.

For instance, if we have a class which defines overloads for both
stringification and concat, some operations will result in new objects
of the class, however others will not when we expect them to.

It would be reasonable to expect these two lines of code to return the
same result:

```perl
# this reduce works as expected with an overloaded concat
my $ret = reduce { ( $a . $sep ) . $b } @list;

# this join() surprisingly uses stringification of $sep,
# even when concat is overloaded
my $ret = join $sep, @list;
```

The reason they do not is that join() invokes stringification, but
should use concat when that function is overloaded by the class of
the object.

Similarly, the substr() builtin is not overloadable and it should be.

```perl
use v5.32;
my $obj1 = OverloadedSubstr->new( "foo" );
my $obj2 = substr( $obj1, 1, 1 );
$obj2 eq 'o'; # true
$obj2 isa OverloadedSubstr; # should be true, but is false
```

If overload included a notion of substr, the package overload::substr
would no longer be needed. https://metacpan.org/pod/overload::substr

In an email conversation, Paul "LeoNerd" Evans wrote the following
regarding overload::substr

> the only reason I made that module in the first place is exactly
> because substr() doesn't support overloading - the module provides
> a monkeypatch to the core OP_SUBSTR to enable it.

Thank you and any feedback is welcome.

Philippe Bruhat & Eric Herman

--
Philippe Bruhat (BooK)

Even the best-made plans of kings can sometimes go awry...
(Moral from Groo The Wanderer #110 (Epic))
Re: Pre-RFC: Support overloaded objects in join(), substr() builtins [ In reply to ]
On Wed, 3 Nov 2021 18:16:34 +0100
"Philippe Bruhat (BooK)" <book@cpan.org> wrote:

> Thank you and any feedback is welcome.

Well, nobody's objected here, and I have quite a vested interest, in my
modules and giving guidance behind the scenes.

So, I say go for it - write it up as a real RFC now :)

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: Pre-RFC: Support overloaded objects in join(), substr() builtins [ In reply to ]
On Fri, Nov 12, 2021 at 03:02:12PM +0000, Paul "LeoNerd" Evans wrote:
>
> So, I say go for it - write it up as a real RFC now :)
>

The RFC is here: https://github.com/Perl/RFCs/pull/9

--
Philippe Bruhat (BooK)

Danger lurks most often when you have an eye out for it.
(Moral from Groo The Wanderer #6 (Epic))
Re: Pre-RFC: Support overloaded objects in join(), substr() builtins [ In reply to ]
On Tue, 23 Nov 2021 09:20:47 +0100
"Philippe Bruhat (BooK)" <book@cpan.org> wrote:

> On Fri, Nov 12, 2021 at 03:02:12PM +0000, Paul "LeoNerd" Evans wrote:
> >
> > So, I say go for it - write it up as a real RFC now :)
> >
>
> The RFC is here: https://github.com/Perl/RFCs/pull/9

LGTM.

If there are no further comments before our PSC meeting on Friday, I
vote we accept this and call it RFC 0010.

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: Pre-RFC: Support overloaded objects in join(), substr() builtins [ In reply to ]
On Wed, 15 Dec 2021 15:57:50 +0000
"Paul \"LeoNerd\" Evans" <leonerd@leonerd.org.uk> wrote:

> LGTM.
>
> If there are no further comments before our PSC meeting on Friday, I
> vote we accept this and call it RFC 0010.

0010 was taken; this is now merged as 0013.

https://github.com/Perl/RFCs/blob/master/rfcs/rfc0013.md

We look forward to seeing an implementation. :)

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/