DIP 1027--String Interpolation--Final Review Discussion Thread

Jacob Carlborg doob at me.com
Wed Feb 5 16:37:52 UTC 2020


On 2020-02-05 16:20, Steven Schveighoffer wrote:

> Note that C# does something pretty cool [1] (as I just looked it up to 
> see what other interpolation libraries do): if the interpolation is 
> intended to be used as a string, it lowers to a call to String.Format. 
> If its type is going to be IFormattable or FormattableString, it 
> generates a structure not dissimilar to what we are wanting here [2]. So 
> there is kind of a precedent for this feature in other string 
> interpolation implementations.
> 
> I'm not sure whether we can provide this level of seamlessness without 
> return type overloads, but I like the idup idea

I've been thinking something similar for quite a while. Here's my idea, 
it's a simple extension to Adam's proposal:

* If the string interpolation expression appears in the code as an 
argument to a function and if that function is declared with the 
@stringInterpolation UDA it generates the struct according to Adam's 
proposal. Example:

@stringInterpolation string sql(T)(T) { ... }
@stringInterpolation string jsx(T)(T) { ... }

float value;
string table;

sql(i"SELECT * FROM $table WHERE value = $value");

Lowered to:

sql(_d_interpolated_string!(...)(...));

* In all other cases the compiler generates the struct according to 
Adam's proposal BUT also calls the `idup` helper function:

auto a = "bar"
auto b = 3;
auto str = i"foo $a $b";

Lowered to:

auto str = _d_interpolated_string!(...)(...).idup;

static assert(is(typeof(str) == string));
assert(str == "foo bar 3");

I think this will be the most common usage, to just convert to a string 
like `std.format.format` would do.

, and I still think we
> shouldn't call this feature string interpolation, due to the existing 
> expectation for "string interpolation". Inspired by that C# type, maybe 
> "formattable tuple" is a good description.

Some version of "string build"? I.e. "string builder", "string building" 
or similar.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list