3 ms·
STR is not some special syntax, just a receiver for a method call. You would only need STR to interpolate a template into a String, but any receiver can specify
by pron 4y ago
STR is not some special syntax, just a receiver for a method call. You would only need STR to interpolate a template into a String, but any receiver can specify that interpretation of the template -- or a different one. So, for example, a logger could use:
log.info."x: \{x}";
and similarly for other uses that don't produce strings but JSON, SQL etc. So STR would only be used -- hopefully rarely -- for old APIs that have not determined their own template interpretation strategy and only accept String.
The use of the backslash is important to distinguish between a string literal and a template literal because "x: \{x}" is not a valid string literal today. Swift uses \(...), BTW.
- Bjartr 4y ago> So STR would only be used -- hopefully rarely If that's the hope, why have it automatically imported statically for all files?
- pron 4y agoBecause it doesn't hurt, and because it will take some time until all relevant libraries expose their chosen template processor, reducing the need for STR, the tax on less restrained uses needs to be neither too high nor too low.
- zaphirplane 4y ago> STR."My name is \{name}"; What is the method call ? Agree with grandparent this looks too inconsistent for Java STR.format(“”) would be more Java with import as Alias
- kaba0 4y agoStringTemplate t = RAW.”My name is \{name}”; STR.process(t)
- zaphirplane 4y agoThat’s special magic if I recall my Java syntax correctly System.out.print.”hello” isn’t a thing
- kaba0 4y agoYes, it is a very tiny new syntax so that templates can be distinguished from string literals (so string templates can never stand without a template processor, hence the RAW)
- optician_owl 4y agoAs far as I understand the backslash is a mark of a variable. The problem that it looks like the escape symbol and this is really confusing. What about another symbol? #,%,|,<... Of course, $ is ideal because it's already everywhere and familiar. I guess a typographer of a font designer would be helpful to find a good solution.
- gergo_barany 4y ago> As far as I understand the backslash is a mark of a variable. The problem that it looks like the escape symbol and this is really confusing. No, it is the escape symbol and works just like the escape symbol because it is the escape symbol. The escape symbol \ means that whatever follows should not be treated literally but interpreted in some special way. So "\n" means "not the letter n but rather a newline", and "\u1234" means "not the letter u followed by the digits 1, 2, 3, 4, but rather the unicode character U+1234". And in exactly the same way, "\{...}" means "not the opening curly brace followed by some stuff and then a closing curly brace, but rather special semantics for the expression (not variable) within the curly braces". > Of course, $ is ideal because it's already everywhere and familiar. It is already everywhere, including in existing Java strings in existing Java code, and the semantics of that existing code must not change. The very fact that it is already everywhere means that it is not ideal in the context of retrofitting this feature onto Java.