4 ms·
Would it hurt them to use ${x} instead of \{x}? I mean, "${x}" could already be a literal in strings before, but they have the <processor>. prefix to the strin
by coldtea 3y ago
Would it hurt them to use ${x} instead of \{x}?
I mean, "${x}" could already be a literal in strings before, but they have the <processor>. prefix to the string, which no old code would have. One needs to actively use it, and so that someone can also actively check not to write an unescaped ${x} when they don't want interpolation (like in other languages that use it).
Also the whole processor for escaping is imho ill-conceived. Escaping for a specific language like SQL should happen on the formatter instructions level, e.g.
"SELECT airport_code FROM airports WHERE name != '%sql.string\{surname}'"
->
SELECT name FROM table_foo WHERE surname != 'O''Hare'
or something to that effect (and similar for sql.identifier etc).
This way you could easily write classes with collections of methods to format a specific kind of language's values - and not have write a full formatter that parses and understands the whole string in context (as I assume is the case now, if you don't just use the naive STR/FMT one, as 99% will do).
Especially since you might need to handle multiple languages escaping rules in the same template, if you include strings for multiple languges/contexts in your template. What you do for that? Write the cartesian combo of template processors and make each understand and parse multi-language string tokens?
- krzyk 3y agoHow is it better? Just different, and using dollar sign adds another shift press.
- coldtea 3y ago>How is it better? Much more older and common use and association (even their own "Java EE Expression Language" uses it), and not confused with escaping a character (like \n, \t) etc. Pricinple of least surprise, avoiding NIH, and stuff.
- pron 3y ago> Much more older and common use and association (even their own "Java EE Expression Language" uses it) That was one of the main reasons not to use it; even in JS templating libraries use different syntax from that of the language's $ (see my other comment). > and not confused with escaping a character (like \n, \t) etc That would not be a confusion at all but the very point: \{ is just like \n or \t, i.e. gets a special interpretation rather than a literal one. > Pricinple of least surprise \ in Java is standard for that purpose (special interpretation in a literal); it is $ that is foreign. > avoiding NIH That is not so much of a principle, although it could be if the language was rejecting some otherwise universal choice, only that's not the case here at all. Of the other popular language with either a similar feature to Java's string templates or with string interpolation -- JS, Python, PHP, C#, and Ruby -- only JS and PHP use $ (and only PHP uses $ with the "shell" shortcut of $x). Or, if you only want to look at the "big three" -- JS, Python, and Java -- each has a different syntax from the other two, and only one uses $. Even if you look at all programming languages in the world, only a minority of programmers using a language with string templates and/or string interpolation use one where $ is used for the value syntax. Not only is it not a near universal choice, it's a minority choice.
- deleted 3y ago[deleted]
- usrusr 3y agoThe language spec is the only place able to introduce meaning to a source byte sequence starting with a backslash that has been invalid before (e.g. not double-backslash, or backslash-u). ${ on the other hand never been an invalid byte sequence inside a string literal, and was hence already used by many APIs. It's nothing but courteous by the language people to avoid avoidable collisions. Keep in mind that this new syntax is completely open to everybody else who might have a use case for strings-with-expressions, it's not like they are appropriating a previously blank spot for exclusive use.
- coltonweaver 3y agoEdit: This comment was written when the original comment was just "Would it hurt them to use ${x} instead of \{x}?" This is covered in the article under "Alternatives", in the section beginning with: > For embedded expressions in string templates, we considered adopting the ${...} syntax from the Java EE Expression Language (EL) rather than \{...}. However, this would force developers to escape every $ sign in the literal text of a string template while continuing to write $ without escapes in string literals and text blocks. This inconsistency could all too easily lead to errors. ... > With the syntax we have chosen, frameworks can ease migration by providing a template processor that understands EL syntax. Developers can continue using ${...} while also having access to the Java environment via \{...}:
- coldtea 3y ago>However, this would force developers to escape every $ sign in the literal text of a string template while continuing to write $ without escapes in string literals and text blocks. The other side is that now they'll make them think they can also write \{foo} in string literals and text blocks, and they'll find out it doesn't work.
- masklinn 3y agoI would assume that will keep being a compilation error. It also seems to be a very "think of the morons" point to raise.
- coltonweaver 3y agoCorrect, this example is also in the JEP: String name = "Joan"; String info = "My name is \{name}"; | error: processor missing from template expression
- pron 3y ago> Would it hurt them to use ${x} instead of \{x}? There are multiple reasons to prefer \: 1. In Java, \ is already the character that signifies that what follows has a special meaning, while the use of $ is foreign (to the language). 2. `\{` is a syntax error inside string literals, which means it appears nowhere inside string literals, while $ is fairly common; it would also mean that $ will need to be written differently inside string and string template literals. 3. The use of $ for interpolation is common in Java templating libraries, and it's preferable for the language to use something different (in JS, where $ is used by the language, templating libraries also use something else, like `{{`). See https://mail.openjdk.org/pipermail/amber-spec-experts/2023-April/003827.html https://mail.openjdk.org/pipermail/amber-spec-experts/2023-A... > or something to that effect (and similar for sql.identifier etc). You can already do that if you want: X."blah \{sqlString(x)} blah"; But this feature is being added to Java to solve the pretty serious problem of code injection, and so the more important thing is the selection of the processor, which ensures safety, i.e. an HTML API will offer an HTML processor and will not accept strings. Remember, string templates are most useful when what they produce is not a string, but rather construct a specific API entity.
- coldtea 3y ago>But this feature is being added to Java to solve the pretty serious problem of code injection My take is that it will do little for code injection. Most programmers will use STR/FMT, and unless they offer an HTML or an SQL processor in the stdlib, they wont be used. >Remember, string templates are most useful when what they produce is not a string, but rather construct a specific API entity Sure, but the processor can do little short of implementing full parsing to understand the context of say HTML or SQL, right?
- pron 3y ago> Most programmers will use STR/FMT, and unless they offer an HTML or an SQL processor in the stdlib, they wont be used. New processors obviously can't be used unless they're offered by a library, but when they are, they must be used. The libraries simply won't offer an API based on strings (and, therefore, interpolated strings), or if they already do, they will be able to deprecate them. If an HTML library has no API that takes strings, then STR/FMT simply can't be used. The library can enforce the safety of its API. > Sure, but the processor can do little short of implementing full parsing to understand the context of say HTML or SQL, right? Yes, that's the point. An HTML library will offer an HTML processor, and similarly for a JSON or SQL library. I'm not apprised of the exact status, but I believe that the team specifying JDBC is looking into offering a SQL processor as part of JDBC.