←back to thread

620 points tambourine_man | 1 comments | | HN request time: 0.212s | source
Show context
franga2000 ◴[] No.43750305[source]
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.

replies(2): >>43750403 #>>43751094 #
conartist6 ◴[] No.43750403[source]
For 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.
replies(2): >>43750893 #>>43751383 #
1. masklinn ◴[] No.43751383[source]
The 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.