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

Walter Bright newshound2 at digitalmars.com
Sat Dec 14 09:21:27 UTC 2019


On 12/13/2019 5:11 AM, Jacob Carlborg wrote:
> * 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));

That doesn't work because semantic analysis will convert it into a tuple.


> * 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 only makes sense if foo is an expression.

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

It works with anything that accepts a tuple.

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

I agree that is the whole point, and the proposal does that.

> * 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)

The interpolation just creates a tuple. What the user does with the tuple is up 
to him.


More information about the Digitalmars-d mailing list