[OT] C# can do all the interpolated strings now

WebFreak001 d.forum at webfreak.org
Thu Dec 9 15:16:40 UTC 2021


On Thursday, 9 December 2021 at 14:57:25 UTC, Steven 
Schveighoffer wrote:
> On 12/9/21 9:25 AM, WebFreak001 wrote:
>
>> Love the idea of having type-safety with this! JS has similar 
>> syntax with `` sql`...` `` where it will call the function 
>> `sql` with the parts of the interpolated string.
>> 
>> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
>> 
>
> This is exactly what the proposal is doing. But instead of 
> using `sql` as a specialized prefix (you have to be careful 
> with these, as adding arbitrary literal prefixes can mess up 
> the grammar) it's just a parameter tuple, and you specify the 
> function as normal.
>
> i.e. this is practically identical to:
>
> ```d
> sql(i"SELECT x,y,z FROM $something WHERE $condition");
> ```
>
> Or even:
>
> ```d
> i"SELECT x,y,z FROM $something WHERE $condition".sql;
> ```
>
> -Steve

I think identifiers immediately followed by a string literal are 
unambiguous. Currently a syntax error, afterwards useful 
interpolation.

The i"" proposal allows to pass multiple arguments at once, and 
with that adds new cases to the compiler that a single literal 
can be multiple arguments. The identifier + string literal stays 
within bounds of existing behavior.

I think you would really really never want to pass these raw 
tuple values to any function other than to explicitly designed 
interpolated string handlers.

Given that assumption I think it's safe to say the `i` prefix is 
redundant, and you should just always need to use a handler 
function. The identifier + string literal syntax elegantly does 
this without redundant syntax, it's familiar to JS programmers 
and it doesn't allow passing more than the interpolated string to 
handler functions or functions with variadic arguments. (which is 
a good thing)

Samples:

```d
sql(i"SELECT x,y,z FROM $something WHERE $condition")
text(i"hello $name")
mixin(i"class $name {}")
```

vs

```d
i"SELECT x,y,z FROM $something WHERE $condition".sql
i"hello $name".text
mixin(i"class $name {}") // no equivalent
```

vs

```d
sql"SELECT x,y,z FROM $something WHERE $condition"
text"hello $name"
mixin"class $name {}"
```

or have some kind of new function call punctuation:

```d
sql$"SELECT x,y,z FROM $something WHERE $condition"
text$"hello $name"
mixin$"class $name {}"
```

---

and if you want to actually access the tuple for variadic 
template arguments (including the header) you could still do this:

```d
AliasSeq"hello $name"
```

(or `alias I = AliasSeq;` and `I"hello $name"`)


More information about the Digitalmars-d mailing list