4 ms·
I wish they added the same thing JS has, where this "string literal prefix thingy" can be user-defined. html`<p>${value}</p>` will actually run the function ht
by franga2000 2y ago
I wish they added the same thing JS has, where this "string literal prefix thingy" can be user-defined.
html`<p>${value}</p>` will actually run the function html(template). This means you can use this to "mark" a function in a way that can be detected by static analysis. Many editors will, for example, syntax highlight and lint any HTML marked this way, same with SQL, GraphQL and probably some others too.
- conartist6 2y agoFor the record the JS thing desugars to the exact same as the Python thing, so it is no more or less safe to do the syntax highlighting in Python as it is in JS.
- Timon3 2y agoIt desugars similarly, but the Python version doesn't have a name. Any t-string is a t-string, there's no HTML t-string or SQL t-string or anything like that. It's just a t-string you can pass to a function: html_string = t"<something />" sql_string = t"SELECT * FROM something" In JS, the string has a prefix that can differ between languages, e.g.: const htmlString = html`<something />` const sqlString = sql`SELECT * FROM something` and so on. See the difference?
- masklinn 2y agoExcept your labels are incorrect because neither `html_string` nor `sql_string` are strings, they're both Template objects, and the sink function is the one which processes it. No processing has happened to them by the end of the snippet, beyond creating the template object itself.
- Timon3 2y agoSure, choose different variable names, who cares. The essential difference is that the language is referenced at the declaration site, not the usage site, which makes the syntax highlighting far easier. Please engage with my point instead of criticizing trivialities.
- masklinn 2y ago> Please engage with my point instead of criticizing trivialities. Your complete misunderstanding of what's happening is not a triviality. > The essential difference is that the language is referenced at the declaration site, not the usage site, which makes the syntax highlighting far easier. Javascript has no built-in template tags beyond `String.raw`. If tooling has the capabilities to infer embedded language from arbitrary third party libraries, I would hope they have the ability to do utterly trivial flow analysis and realise that html(t"<something />") means the template string is pretty likely to be HTML content.
- Timon3 2y agoCome on, you're just being rude for no good reason. A badly chosen variable name doesn't show a "complete misunderstanding". Yes, the variable should have been named `html_template` instead of `html_string` - how often do I have to acknowledge this before you accept it? And it's obviously more complex to do syntax highlighting when the declaration site and usage site are possibly split apart by variable assignments etc. Yes, in the case you showed syntax highlighting is easy, but what if the `html` function takes more parameters, doesn't take the template as the first parameter, etc? There's a lot of possible complexity that tagged template literals don't have. Thus they are easier to do highlighting for. This is objectively true.
- conartist6 2y agoTagged template literals in JS have all that complexity. All of it. That tools you trust lie and pretend that's not the case doesn't make the language spec say anything different
- Timon3 2y agoNo, they literally don't, because they don't support these features! You can't split apart the declaration of the template literal and the "tagging". The tag is always part of the declaration, which it doesn't have to be in Python, as I've showed. You can't pass additional parameters to the tag function, it's always just the template & values. In Python, you can pass as many parameters as you want to the usage site, e.g. some_value = html(True, t"<something />", 42)
- masklinn 2y agoThe JS version actually desugars to something much more primitive, and less convenient: in JS a template tag receives one parameter which is an array of n+1 strings, and then n parameters for the interpolated values, and has to iterate alternatively on both sequences. You can do that in python by accessing the `strings` and `values`, but I expect most cases will simply iterate the template, yielding a unified typed view of literal strings and interpolated values.
- nhumrich 2y agoThe PEP was initially proposed this way. But due to various reasons, making it an open namespace was considered to overcomplicate the language (read: understanding coffee when reading it). Alternatively, there doesn't really seem to be much loss of ability with t-strings. Libraries can require a template as it's accepted type, instead of having to invent their own custom type and named template.