4 ms·
I tried skimming the PEP while I could, but it seems like this might be missing a couple of the features that make JS tagged template literals work so well: -
by spankalee 2y ago
I tried skimming the PEP while I could, but it seems like this might be missing a couple of the features that make JS tagged template literals work so well:
- tags get a strings array that's referentially stable across invocations. This can function as a cache key to cache strings parsing work.
- tags can return any kind of value, not just a string. Often you need to give structured data to another cooperating API.
Deferred evaluation of expressions is very cool, and would be really useful for reactive use-cases, assuming they can be evaluated multiple times.
- svieira 2y agoThe string array is not referentially stable across invocations and cannot be because there is a single argument array containing both the "static" bits and the "dynamic" bits. So you can't use it the way that JS' `static_strings` argument can be used as a key in a `WeakMap`. Tags can return any kind of value, so there's that. Deferred evaluations can be evaluated multiple times and is in fact one of the biggest foot-guns in this API (in my opinion).
- spankalee 2y agoI wonder if each of the string args is stable? Then you could just use the first as the key. The JS API where the strings are separate might seem awkward at first, but it ends up being a really elegant design. Deferred evaluation is really powerful and I wish JS had it. It's one of the reasons why Solid has a custom JSX compiler and doesn't use tagged literals... to get the-evaluation you need to user to pass in closures, which is cumbersome.
- Nullabillity 2y agoPython does string interning, so that seems.. dangerous.
- spankalee 2y agoMost managed languages do string interning. But the object passed as the string value doesn't seem to be just a string. It's wrapped in another object, which could have callsite identity.
- jimbaker 2y agoThanks to the feedback in the Discourse thread, we will be changing the signature such that a tag function will be passed an object implementing a Template protocol, with attributes source, for the original source string as written; and attribute args for what was *args in the starting version of the PEP. Template.source can act as a memoization key.