Re: [RFC] Better string interpolation
| From: | Ilija Tovilo | Date: | Sun, 16 Aug 2020 17:45:36 +0000 |
| Subject: | Re: [RFC] Better string interpolation | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111561@lists.php.net to get a copy of this message | ||
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