Draco-lang / Language-suggestions

Collecting ideas for a new .NET language that could replace C#
75 stars 5 forks source link

String interpolation #53

Closed LPeter1997 closed 2 years ago

LPeter1997 commented 2 years ago

Note: This proposal doesn't intend to solve multiline string literals. This is purely for one-liner strings.

Introduction

String interpolation allows placeholders to be added to string literals that get filled in by stringified expressions. It's generally faster than raw string concatenation in languages with immutable strings, as the compiler usually turns interpolated strings into an expression using a more efficient builder type (something like StringBuilder in C#).

For example, in C#

$"1 + 2 = {1 + 2}"

will be evaluated to the string "1 + 2 = 3" and the compiler underneath could have generated something like:

var sb = new StringBuilder();
sb.Add("1 + 2 = ");
sb.Add((1 + 2).ToString());
var str = sb.ToString();

(Note, that it's probably not exactly this in C#)

Syntax variations

This is one of the few features that has relatively few syntax variations among mainstream languages. Main variables between syntaxes:

To showcase how this looks like in different languages, we'll assume that a local variable named x is defined, and we want to write the equivalent interpolated string to x.ToString() + " + 1 = " + (x + 1).ToString().

Proposal for Fresh

I believe there is no reason not to have string interpolation enabled by default, so I'd say there's no need for a prefix character, like $ in C#. For the interpolation syntax, the one used by Kotlin (and many other languages) seems the cleanest to me, so I suggest that. This introduces a new escape sequence, \$, but that shouldn't be an issue.

Possible conflict in language features

Currently #41 suggest every nonstatic member has to be prefixed with this. This means, that interpolating members has to be with the more verbose, ${...} syntax (otherwise, something like $this.x would be equivalent to this.ToString() + ".x"). For now, I don't think this is such a big issue, but when making the final decision, we might decide to use a different syntax because of this.

WhiteBlackGoose commented 2 years ago

This introduces a new escape sequence, \$, but that shouldn't be an issue.

Which, practically, doesn't ever have to be escaped because we don't write ${ or $a (unless interoplate), whereas in all other cases, like $1000 or $ 1000 or etc. it will be interpreted as $ character

LPeter1997 commented 2 years ago

@WhiteBlackGoose That is true, but this is important for the same reason as \' being ' in string literals, simpler metaprogramming and code generation. It's simpler to have a single function in our standard library that "stupidly" escapes all characters that are allowed to be escaped than to start differentiating in different contexts.