DIP 1027---String Interpolation---Community Review Round 1

Jacob Carlborg doob at me.com
Fri Dec 13 13:11:40 UTC 2019


On Wednesday, 11 December 2019 at 09:52:21 UTC, Mike Parker wrote:
> This is the feedback thread for the first round of Community 
> Review for DIP 1027, "String Interpolation":

In my opinion, the semantics of string interpolation need to 
support:

* The type of an interpolated string should be the same as a 
regular string. That means that the following should work:

auto foo = 3;
auto b = i"%foo";
static assert(is(typeof(b) == string));

* It should support both interpolating expressions and, with a 
shorter syntax, symbols, example:

auto foo = 3;
auto bar = 4;
auto a = i"%foo" // interpolation of symbol
auto b = i"%(foo + bar)" // interpolation of expression

* It should not be tied to specific functions, like `printf` or 
`writef`

* It needs to be possible to identify which part is a literal 
string and which part is an interpolation

* A way to customize how the interpolation is performed. This I'm 
not sure how to do best. There are a couple of alternatives. In 
Swift string interpolation is lower to method calls [1]:

"The time is \(time)."

Is lowered to something like:

var interpolation = MyString.StringInterpolation(literalCapacity: 
13,
                                                  
interpolationCount: 1)

interpolation.appendLiteral("The time is ")
interpolation.appendInterpolation(time)
interpolation.appendLiteral(".")

MyString(stringInterpolation: interpolation)

When it comes to the syntax. I think the existing suggestion to 
use `%` is bad, it's just going to cause conflicts with format 
specifiers for functions like `printf`. I would prefer to use the 
dollar sign, i.e. `$foo` and `$(a + b)`.

[1] 
https://developer.apple.com/documentation/swift/stringinterpolationprotocol

--
/Jacob Carlborg



More information about the Digitalmars-d mailing list