4 ms·
Looks pretty good. But I don’t understand why they don’t do string interpolation like C# and others do. “I= {I}” Is much better and less error prone than “I=
by Ididntdothis 7y ago
Looks pretty good. But I don’t understand why they don’t do string interpolation like C# and others do.
“I= {I}”
Is much better and less error prone than
“I= {}”, I
Or am I missing something?
- _hardwaregeek 7y agoProbably some syntactical reason. If you allow arbitrary expressions inside string interpolation, you'd need to have some form of state in your lexer to figure out when to lex the characters into tokens or just keep them as raw characters. It's not so easy as looking for another brace, as you can have expressions that have braces (lambdas). Plus, string interpolation can then be nested, which you may not want.
- royjacobs 7y agoI don't think you'd be able to evaluate your expression without some kind of runtime reflection. Specifically, how is a standard library method supposed to interpolate {l}? It doesn't have any information about which variables are present in some kind of format string.
- Ididntdothis 7y agoI am pretty sure C# does the interpolation at compile time but it probably helps that it has a string type as first class citizen to the compiler has more information.
- plorkyeran 7y agoThe format library is a pre-existing library which is being standardized. String interpolation cannot be implemented in a library, and requires language extensions. Perhaps they should have done that instead, but that's a significantly more complicated task than merely adopting something that's already well tested and proven (and even that was far from trivial).
- hawski 7y agoI am not necessarily opposing string interpolation as a language feature, but I have an idea how could it work as a library. Implement string suffix and overload comma operator or overload reminder operator. It would look like this: "Hello {} {}!"i, 2, "worlds" Or with reminder operator as in Python: "Hello {} {}!"istr % istr(2, "worlds") But yeah, enough of this.
- bsder 7y agoC++ (and C) have a fairly odd characteristic in that it is really hard to definitively figure out what is an identifier. It's one of the reasons C++ takes so long to compile. It's why the "modern" languages all seem to be converging on having an explicit token that signals "This thing is an identifier : this thing is a declaration".