4 ms·
Swift string interpolation syntax is not more awesome or any less ambiguous than other syntaxes used in other languages In fact, it's the first time that I've
by berdario 10y ago
Swift string interpolation syntax is not more awesome or any less ambiguous than other syntaxes used in other languages
In fact, it's the first time that I've seen interpolation with round parentheses, rather than curly braces (which are used in javascript with $ (es2015 template literals), python, bash with $, ruby with #, groovy with $, etc... )
> LoL. Refcounting can lead to just as long pauses as GC
He didn't say more performant, he said consistent
- tomp 10y ago(1) It's obviously less ambiguous, because you can't do it accidentially; "\(" is an invalid escape, whereas "${1}" or "$a" is a valid string (i.e. you could write it without meaning to interpolate the variables). (2) It's only consistent if you always know what your pointers are pointing at, and whether your pointers are the last reference to it... but then you can just use delete, right?
- berdario 10y ago(1) you're confusing C string and other languages strings (even Swift strings are not the same thing as NSString or C strings). Anyhow, even in C \( is perfectly accepted by compilers gcc -Wall -x c - <<EOF #include <stdio.h> int main(){ printf("\(hi"); } EOF So it's perfectly reasonable to write it and not mean interpolation (maybe they meant "\\(", but that's another matter) (2) A sequence can be sorted without you knowing which elements are inside it, A computation can be known to terminate without you knowing (yet) which value it will yield. Similarly, you don't need to have internal knowledge of your code to know that you'll be able to rely on RC consistency in the future (otoh I guess that something consistent is not necessarily the same as something predictable)
- hk__2 10y ago> Anyhow, even in C \( is perfectly accepted by compilers Even if it’s accepted it’s not valid, at least in the C99 standard.
- 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."