rfc:arbitrary_string_interpolation

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:arbitrary_string_interpolation [2022/03/17 19:15]
ilutov
rfc:arbitrary_string_interpolation [2022/06/25 21:47] (current)
ilutov Move to wirthdrawn
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: Withdrawn
   * Target Version: PHP 8.2   * Target Version: PHP 8.2
-  * Implementation: https://github.com/php/php-src/compare/master...iluuu1994:arbitrary-string-interpolation?expand=1+  * Implementation: https://github.com/php/php-src/pull/8256
  
 ===== Proposal ===== ===== Proposal =====
Line 16: Line 16:
   * 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 from other forms of string interpolation to avoid confusion. The '':'' could be swapped with another character or even whitespace, but I believe that whitespace has a bigger chance of leading to confusion.+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. A different syntax could be chosen with a given BC break.
Line 76: Line 76:
 I think this is unfortunate because: I think this is unfortunate because:
  
-  * Converting an existing string to ''$'' will potentially alter the behavior and require escaping other parts of the string (similar to switching from ''''' to ''"''+  * 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)+  * 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 103: 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 =====
  
-Credits to Rowan Tommins (IMSoP) for the syntax suggestion.+Credits to Rowan Tommins (IMSoP) for the syntax suggestion and comparison to other languges.
  
 ===== Vote ===== ===== Vote =====
rfc/arbitrary_string_interpolation.1647544502.txt.gz · Last modified: 2022/03/17 19:15 by ilutov