rfc:arbitrary_string_interpolation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:arbitrary_string_interpolation [2022/03/17 18:56] ilutovrfc:arbitrary_string_interpolation [2022/03/27 16:17] – Add comparison to other languages ilutov
Line 3: Line 3:
   * Date: 2022-03-17   * Date: 2022-03-17
   * Author: Ilija Tovilo, tovilo.ilija@gmail.com   * Author: Ilija Tovilo, tovilo.ilija@gmail.com
-  * Status: Darf+  * Status: Under discussion
   * Target Version: PHP 8.2   * Target Version: PHP 8.2
-  * Implementation: TBD+  * Implementation: https://github.com/php/php-src/pull/8256
  
 ===== Proposal ===== ===== Proposal =====
Line 11: Line 11:
 PHP has four types of string interpolation. The two most common forms are ''"$foo"'' and ''"{$foo}"''. For details on the two other forms of string interpolation see the [[https://wiki.php.net/rfc/deprecate_dollar_brace_string_interpolation|deprecate ${} string interpolation RFC]]. PHP has four types of string interpolation. The two most common forms are ''"$foo"'' and ''"{$foo}"''. For details on the two other forms of string interpolation see the [[https://wiki.php.net/rfc/deprecate_dollar_brace_string_interpolation|deprecate ${} string interpolation RFC]].
  
-All the current forms of string interpolation are limited to variables or variable chains (array access, properties, method calls, etc). This probably covers 90% of use cases for string interpolation. However, there are times when this is not enough, namely:+All the current forms of string interpolation are limited to variables or variable chains (array access, properties, method calls, etc). This probably covers 80% of use cases for string interpolation (unverified figure). However, there are times when this is not enough, namely you might want to embed any of the following in your string:
  
   * Function calls   * Function calls
   * Constants   * Constants
   * Class constants   * Class constants
-  * Statis method calls+  * Static method calls
  
 <code php> <code php>
Line 38: Line 38:
 </code> </code>
  
-The proposed syntax works in double quoted strings (''""''), heredoc (<<<TXT TXT) and the execution operator (''``'')+The proposed syntax works in all cases where existing string interpolation is allowed, namely double quoted strings (''""''), heredoc (''<<<TXT TXT'') and the execution operator (''``'').
  
 ===== Motivation ===== ===== Motivation =====
Line 44: Line 44:
 Of course, this can already be achieved in two other ways: Of course, this can already be achieved in two other ways:
  
-  1. By creating a temporary local variable +  By creating a temporary local variable 
-  2. By stopping the string and concatenating the value with ''.''.+  By stopping the string and concatenating the value with ''.''
  
 Creating local variables is not a terrible option. It does pollute the variable symbol table a bit and it requires more visual jumping to see what value is being embedded. Creating local variables is not a terrible option. It does pollute the variable symbol table a bit and it requires more visual jumping to see what value is being embedded.
Line 60: Line 60:
 ===== Syntax choice ===== ===== Syntax choice =====
  
-The primary advantage of the syntax ''"{$:expr}"'' is that is it is fully backwards compatible as it leads to a syntax error in PHP 8.1 rather than being interpreted literally. It also looks familiar while being distinctly different to avoid confusion with other forms of string interpolation.+The primary advantage of the syntax ''"{$:expr}"'' is that is it is fully backwards compatible as it leads to a syntax error in PHP 8.1 rather than being interpreted literally. It also looks familiar while being distinctly different from other forms of string interpolation to avoid confusion. The '':'' could be swapped with another character or even white space, but I believe that white space has a bigger chance of leading to confusion. 
 + 
 +A different syntax could be chosen with a given BC break. 
 + 
 +<code php> 
 +echo "Hello #{strtoupper('world')}"; 
 +</code> 
 + 
 +This could be mitigated by adding a prefix to the string itself. 
 + 
 +<code php> 
 +echo $"Hello #{strtoupper('world')}"; 
 +</code> 
 + 
 +I think this is unfortunate because: 
 + 
 +  * Converting an existing string to ''$'' will potentially alter its behavior and require escaping of other parts of the string (similar to switching from ''''' to ''"''
 +  * The prefix will need to work for all existing string types that allow interpolation (''"'', heredoc, execute operator), doubling the types of interpolated string we have
  
 ===== Escaping ===== ===== Escaping =====
Line 86: Line 103:
  
 None None
 +
 +===== Comparison to other languages =====
 +
 +Most modern languages have string interpolation that allow arbitrary expressions.
 +
 +^ Language                                                                                           ^ Syntax                        ^
 +| [[https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated|C#]]       | ''$"x + y = {x + y}"''        |
 +| [[https://api.dart.dev/stable/2.16.1/dart-core/String-class.html|Dart]]                            | '''x + y = ${x + y}'''        |
 +| [[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals|JavaScript]] | ''`x + y = ${x + y}`''        |
 +| [[https://kotlinlang.org/docs/basic-types.html#string-templates|Kotlin]]                           | ''"x + y = ${x + y}"''        |
 +| [[https://peps.python.org/pep-0498/|Python]]                                                       | ''f'x + y = {x + y}'''        |
 +| [[https://docs.raku.org/language/quoting.html#Interpolation:_qq|Raku (Perl6)]]                     | ''"\$x + \$y = { $x + $y }"'' |
 +| [[http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html|Ruby]]                 | ''"x + y = #{x + y}"''        |
 +| [[https://docs.scala-lang.org/overviews/core/string-interpolation.html|Scala]]                     | ''s"x + y = ${$x + $y}"''     |
 +| [[https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html#ID292|Swift]]          | ''"x + y = \(x + y)"''        |
 +
 +===== Credits =====
 +
 +Credits to Rowan Tommins (IMSoP) for the syntax suggestion and comparison to other languges.
  
 ===== Vote ===== ===== Vote =====
rfc/arbitrary_string_interpolation.txt · Last modified: 2022/06/25 21:47 by ilutov