Re: Re: [RFC] Better string interpolation
| From: | Alexandru Pătrănescu | Date: | Sun, 16 Aug 2020 19:07:29 +0000 |
| Subject: | Re: Re: [RFC] Better string interpolation | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111562@lists.php.net to get a copy of this message | ||
On Sun, Aug 16, 2020, 20:45 Ilija Tovilo <tovilo.ilija@gmail.com> wrote:
> Hi internals
>
> > I've been thinking about ways to improve string interpolation.
>
> Absolutely overwhelmed by the feedback (:P) I've decided to create a small
> POC:
>
>
> https://github.com/php/php-src/compare/master...iluuu1994:string-interpolation
>
> The POC uses the following syntax:
> echo "Static method call: #{Foo::bar()}";
>
> Two questions arose:
>
> 1. String prefix
>
> To mitigate the BC break we could require strings that use the new
> interpolation to be prefixed.
>
> // Continues behaving the same
> echo "#{Foo::bar()}";
> // Actually makes use of the new interpolation
> echo $"#{Foo::bar()}";
>
> The main downside is that we have yet another type of string: Simple
> quotes with no interpolation, double quotes with *some* interpolation
> and fully interpolated strings ($""), each one with their heredoc
> counterpart. It's unfortunate but we might prefer this approach to
> mitigate the BC break.
>
> Let me know if you prefer a prefix or no prefix. If the answers are
> inconclusive this might become a secondary vote in the RFC.
>
> 2. Escaping
>
> It's not quite obvious how escaping should behave.
>
> $foo = 'foo';
> echo "\#{$foo}";
>
> // Could print
> //> 1. #foo
> // or
> //> 2. #{$foo}"
>
> We could 1. make the backslash escape just the hash and interpret
> {$foo} as usual, or 2. make it escape both.
>
> * Option 1 is more consistent with the rest of the language, backslash
> normally just escapes the next character
> * Option 1 requires two backslashes when result 2 is desired ("\#{\$foo}")
> * Option 2 makes it impossible to achieve result 1 with just braces
> and would have to be written as "\##{$foo}"
>
> Let me know which option makes more sense to you.
>
> Ilija
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
Hi Ilija,
I think this could work and thank you very much for the effort into
clarifying the current interpolation situation.
Using a prefix for strings that have a saner interpretation reminds me of
f-strings in python 3. I would suppose you know about it already but I'll
leave a link here anyway: https://www.python.org/dev/peps/pep-0498/
Using $ for the prefix might not be the best option as it would look like
this:
echo $"next is {$i + 1}";
Maybe f can be used similar with how it is in python but I didn't gave too
much thought into it. I'm a casual python user so I need to understand more
the history how they got to it.
Replying from my phone, sorry if my answer is too short, incomplete and
with possible spelling mistakes.
Regards,
Alex