Mailing List Archive

PrePPC - builtin::stringify, builtin::numify
While we've been looking at tidying up a few things around join() and
respecting concat overload, we encountered a weird case.

Code such as

join "sep", $x, $y, $z

will call OP_JOIN on the given input list and thus (at least under our
fixes) will do things like using overload magic if it exists. But when
there is a single item in the "list", code such as

join "sep", $x

will bypass it. This does not get compiled into an OP_JOIN but instead
directly becomes an OP_STRINGIFY - an opcode that exists in core perl
but doesn't have convenient syntax for it. OP_STRINGIFY directly
squashes a value down to a plain perl string, removing any
overload/magic/etc.. that might be on it. This seems perhaps surprising.

Similarly, in qq() strings, code such as:

"$x$y"

will invoke concat magic between $x and $y and return the result of
that even if it is not a plain string, but code such as:

"$x"

will directly invoke OP_STRINGIFY on the value, squashing it down to a
plain string and removing the magic.

We feel that these are weird cornercases that ideally should behave in
a more flexible, predictable and consistent manner. It suggests that it
would be nice to have a real builtin function to request that a value
get squashed down to a plain string - by invoking the '""' overload if
necessary. There's already a core OP_STRINGIFY to invoke here, so it
seems simple enough to wrap it as a new builtin - builtin::stringify.
If perl provided such a stringify function, then in the presence of the
"please make join respect overloading" flag that one-argument
specialcase can be removed, as now that special behaviour is no longer
required.


And while we're there, it would seem useful to have a similar one to
squash a value down to a plain number, by invoking the '0+' overload if
necessary. There isn't even valid perl syntax for doing this right now
- hacks such as adding zero really just invoke the '+' overload and
thus may still yield an overloaded or magic value. It'd be nice to have
a builtin::numify for this purpose.


Shall I write up a PPC document on adding these two?


* No don't do that at all

* Write a full PPC

* Just proceed directly to the PR and implement them already!

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Re: PrePPC - builtin::stringify, builtin::numify [ In reply to ]
On 31 Aug 2023, at 16:56, Paul LeoNerd Evans <leonerd@leonerd.org.uk> wrote:
> We feel that these are weird cornercases that ideally should behave in
> a more flexible, predictable and consistent manner. It suggests that it
> would be nice to have a real builtin function to request that a value
> get squashed down to a plain string - by invoking the '""' overload if
> necessary. There's already a core OP_STRINGIFY to invoke here, so it
> seems simple enough to wrap it as a new builtin - builtin::stringify.
> If perl provided such a stringify function, then in the presence of the
> "please make join respect overloading" flag that one-argument
> specialcase can be removed, as now that special behaviour is no longer
> required.

Frankly, I want this anyway, because I’m tired of remembering whether $some_object->method->other_method->some_other_object wants to be stringified with ->to_string, ->as_string, ->stringify or whatever.

(And because the object is something I got as a result of a bunch of method calls, I can’t just wrap it in double quotes, because interpolation won’t kick in.)

Sam
--
Website: http://www.illuminated.co.uk/
Re: PrePPC - builtin::stringify, builtin::numify [ In reply to ]
On Thu, Aug 31, 2023 at 7:23?PM Sam Kington <sam@illuminated.co.uk> wrote:

> On 31 Aug 2023, at 16:56, Paul LeoNerd Evans <leonerd@leonerd.org.uk>
> wrote:
>
> We feel that these are weird cornercases that ideally should behave in
> a more flexible, predictable and consistent manner. It suggests that it
> would be nice to have a real builtin function to request that a value
> get squashed down to a plain string - by invoking the '""' overload if
> necessary. There's already a core OP_STRINGIFY to invoke here, so it
> seems simple enough to wrap it as a new builtin - builtin::stringify.
> If perl provided such a stringify function, then in the presence of the
> "please make join respect overloading" flag that one-argument
> specialcase can be removed, as now that special behaviour is no longer
> required.
>
>
> Frankly, I want this anyway, because I’m tired of remembering whether
> $some_object->method->other_method->some_other_object wants to be
> stringified with ->to_string, ->as_string, ->stringify or whatever.
>
> (And because the object is something I got as a result of a bunch of
> method calls, I can’t just wrap it in double quotes, because interpolation
> won’t kick in.)
>

Agreed, both functions sound generally quite convenient

-Dan
Re: PrePPC - builtin::stringify, builtin::numify [ In reply to ]
On 8/31/23 19:44, Dan Book wrote:
> On Thu, Aug 31, 2023 at 7:23?PM Sam Kington <sam@illuminated.co.uk> wrote:
>
> On 31 Aug 2023, at 16:56, Paul LeoNerd Evans
> <leonerd@leonerd.org.uk> wrote:
>> We feel that these are weird cornercases that ideally should
>> behave in
>> a more flexible, predictable and consistent manner. It suggests
>> that it
>> would be nice to have a real builtin function to request that a value
>> get squashed down to a plain string - by invoking the '""'
>> overload if
>> necessary. There's already a core OP_STRINGIFY to invoke here, so it
>> seems simple enough to wrap it as a new builtin - builtin::stringify.
>> If perl provided such a stringify function, then in the presence
>> of the
>> "please make join respect overloading" flag that one-argument
>> specialcase can be removed, as now that special behaviour is no
>> longer
>> required.
>
> Frankly, I want this anyway, because I’m tired of remembering
> whether $some_object->method->other_method->some_other_object
> wants to be stringified with ->to_string, ->as_string, ->stringify
> or whatever.
>
> (And because the object is something I got as a result of a bunch
> of method calls, I can’t just wrap it in double quotes, because
> interpolation won’t kick in.)
>
>
> Agreed, both functions sound generally quite convenient
>
> -Dan

+1
Re: PrePPC - builtin::stringify, builtin::numify [ In reply to ]
On Thu, 31 Aug 2023 19:44:37 -0400
Dan Book <grinnz@gmail.com> wrote:

> Agreed, both functions sound generally quite convenient
>
> -Dan

In that case, here's the first (Easier) part of that; a PR to just
provide `builtin::stringify` that wraps the existing OP_STRINGIFY.

https://github.com/Perl/perl5/pull/21446

--
Paul "LeoNerd" Evans

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