←back to thread

620 points tambourine_man | 1 comments | | HN request time: 0.208s | 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 #
Timon3 ◴[] No.43750893[source]
It 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?
replies(1): >>43751396 #
masklinn ◴[] No.43751396[source]
Except 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.
replies(1): >>43751708 #
Timon3 ◴[] No.43751708[source]
Sure, 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.

replies(2): >>43752287 #>>43752530 #
masklinn ◴[] No.43752287[source]
> 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.
replies(3): >>43752483 #>>43752566 #>>43752846 #
Timon3 ◴[] No.43752483[source]
Come 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.

replies(1): >>43752573 #
conartist6 ◴[] No.43752573[source]
Tagged 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
replies(1): >>43752753 #
Timon3 ◴[] No.43752753[source]
No, 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)
replies(1): >>43753509 #
conartist6 ◴[] No.43753509[source]
It just sounds like you don't know JS very well, because in JS you can definitely split apart the declaration and the tag with ease. The thing implementing the tag is just a function after all: https://jsfiddle.net/rd2f1kot/
replies(1): >>43753614 #
Timon3 ◴[] No.43753614[source]
Sorry, but do just not want to understand what I'm talking about? Your example doesn't show what you're saying it does.

In Python you can do this:

    bar = "hello world"
    template = t"<something foo={bar} />"
    string = html(template)
This is simply not possible in JS, because the template literal always must have the tag attached. You can't split them apart. If you try:

    const bar = "hello world"
    const template = `<something foo=${bar} />`
you already have a string in the `template` variable. There's no access to the individual values anymore. It's already done. It's a string. No template. You can't pull apart the literal declaration and the tagging.

Are we now done with this ridiculous game of deliberate misunderstandings?

replies(2): >>43754328 #>>43754585 #
conartist6 ◴[] No.43754328[source]
I just don't see the same thing as you. This is what you say is simply not possible:

  let passthrough = (...args) => args;
  let bar = "hello world";
  let template = passthrough`<something foo=${bar} />`;
  string = html(...template);
replies(1): >>43754422 #
Timon3 ◴[] No.43754422[source]
So now we have moved from "tagged template literals have all the complexity of Python templates" to "if you define a specific template tag and use that, you can replicate one feature of Python templates". No mention of the other example I mentioned (which, by the way, wasn't an exhaustive list).

Now, I'll just ignore that you're still deliberately misrepresenting me in the hopes of gaining some actual knowledge from this discussion - please show me: which editor supports syntax highlighting for the specific example you just mentioned? After all, that was the topic of discussion - not whether it's possible to delay "tagging", but whether it's easier to do syntax highlighting for JS template tags. Please show me an existing editor, IDE or git repo for a tool that would highlight your specific example based on the `html(...template)` call in line 4 (so no heuristics based on line 3). Surely you're not just throwing random examples at the wall to see what sticks, right? You've actually been following the discussion and are arguing in the context of it?

replies(1): >>43755369 #
1. conartist6 ◴[] No.43755369[source]
Oh now I can reply. Thread continued above: https://news.ycombinator.com/item?id=43754585