4 ms·
> I don't understand why anyone would still require special notation for interpolated strings, when we have the awesome and unambiguous string interpolation syn
by vorg 10y ago
> I don't understand why anyone would still require special notation for interpolated strings, when we have the awesome and unambiguous string interpolation syntax introduced by Swift
You're both wrong. String interpolation is just a very thin syntactic layer. It's easy enough to write
printf("Half of %f is: %f5.2", someVal, someVal * 0.5)
instead of
print("Half of $someVal is: ${someVal * 0.5}")
and a lot easier for a programming language to parse, without needing to feed the parser into the lexer. And you also get easy formatting of the decimal (e.g. %f5.2), whereas string interpolation (in Swift, Apache Groovy, etc) requires a separate call, e.g.
print("Half of $someVal is: ${format(someVal * 0.5, "%f5.2")}")
- drjeats 10y agoC#'s new interpolation syntax is a counterexample to your point about easy formatting: https://msdn.microsoft.com/en-us/library/dn961160.aspx#Anchor_1 https://msdn.microsoft.com/en-us/library/dn961160.aspx#Ancho... $"Name = {name}, hours = {hours:hh}" var s = $"hello, {name}" System.IFormattable s = $"Hello, {name}" System.FormattableString s = $"Hello, {name}" $"{person.Name, 20} is {person.Age:D3} year {(p.Age == 1 ? "" : "s")} old."