5 ms·
The syntax for the string template surprised me. Are there other languages that use a `STR.` prefix to indicate string formatting?
by LatticeAnimal 3y ago
The syntax for the string template surprised me. Are there other languages that use a `STR.` prefix to indicate string formatting?
- xwowsersx 3y agoI don't believe so.
- papercrane 3y agoIt kind of reminds me of JS tagged templates syntax. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- TrianguloY 3y agoPython requires a prefix too f"{x} + {y} = {x+y}". I guess you can now almost simulate it by using f."\{x}...". The new java way is strange, but maybe it was to avoid having to touch the language parser too much.
- mdaniel 3y agothe Java one is pluggable, whereas if one wanted to implement the cited PreparedStatement-strings in Python it'd be a parser bump since there is no `sql""` in the grammar
- TrianguloY 3y agoWhat I wanted to mention is that maybe (although probably not possible with the current grammar) an alternative would have been to remove the dot. STR"x=\{x}"
- mdaniel 3y agoPython does so via its "f-strings": https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals https://docs.python.org/3/reference/lexical_analysis.html#fo... although as the link implies, that's done at the grammar level, and not via a static field that is a StringTemplate.Processor as it is in Java https://openjdk.org/jeps/430#The-STR-template-processor https://openjdk.org/jeps/430#The-STR-template-processor
- munificent 3y agoThe "STR" isn't syntax, it's the name of template processor the user has chosen to process that template. Users can define their own template processors to control how the interpolation behaves: sanitize inputs, etc.
- The_Colonel 3y ago> Users can define their own template processors to control how the interpolation behaves: sanitize inputs, etc. This part honestly seems like overengineering. Like Java architects resisted operator overloading for decades, and now they pull off this in the first version of templated strings ...
- paulddraper 3y agoI disagree. This has existed in JavaScript and Scala for years, and is a very useful feature. Like for SQL queries: sql`SELECT * FROM account WHERE name = ${name}`
- The_Colonel 3y agoJava is a much more conservative language than either JavaScript or Scala. It seems like almost DSL-like feature which is very uncharacteristic for Java. Personally I write JavaScript daily, and I've never seen this feature being used. Even this example - will this create a prepared statement or insert a correctly escaped value into the SQL string? Either way this seems pretty niche. Are there some major use cases for it?
- paulddraper 3y ago> Personally I write JavaScript daily, and I've never seen this feature being used. I see it be used for SQL specifically in loads of projects. https://www.npmjs.com/package/sql-template-strings https://www.npmjs.com/package/sql-template-strings https://www.npmjs.com/package/squid https://www.npmjs.com/package/squid > will this create a prepared statement or insert a correctly escaped value into the SQL string? Always prefer server-side parameterization. If you are referring to java.sql.PreparedStatement specifically, that requires a connection, so this is only an intermediate value to creating that.
- square_usual 3y agoYou can read the JEP for a full list of inspiration and why they chose to do it this way: https://openjdk.org/jeps/430 https://openjdk.org/jeps/430.