@dnolen Proposal for template strings in CLJS. Simply use str + tag for template fn.
(str 1 2 x) => `12${cljs.core.str.arity1(x)` ;; pseudo-code: intent is to coerce nil into empty string
^my-template-fn (str 1 2 x) => cljs.user.my_template_fn`12${x}`
Demo:
cljs.user=> (defn my-template-fn [xs & ys] (prn xs ys) (str (apply str xs) (apply str ys)))
#'cljs.user/my-template-fn
cljs.user=> (let [x 1] ^my-template-fn (str 1 2 x))
#js ["12" ""] (1)
"121"POC commit here: https://github.com/borkdude/clojurescript/commit/ac3935a6fb92e217e7d2dd81e2a4164bf6034bb4
The tag metadata is already used and should not get a second meaning especially for str only. Especially since its also already in use in clojure. So, I'd veto this based on that.
I also do not like adding this to str at all, since CLJ doesn't do that either.
the tag mechanism I mean. str using string templates under the hood could be fine, needs benchmarks to confirm it is generally equal or faster on all supported engines.
needs benchmarks to confirm it is generally equal or faster on all supported engines.I've benchmarked this in the past and this seemed to be the case (but of course we need to do this again if we would pursue this)
Just changing str to template strings doesn't really have any benefits
yeah I'm not hot on adding anything to str - I agree that overloading type hints is probably too much.
Are there any specific problems w/ js-template other than some verbosity? And the verbosity bit could be resolved w/ a different name.
API-wise it's pretty similar to str apart from the first arg. I don't know why it needs another special form in shadow though, it can be done using a regular macro like str currently is written.
Also shadow-cljs's js-template doesn't seem to inline constants other than strings which could be important for stuff like lit.
cljs.user=> (str (fn [] (js-template str 1 2 3)))
"function (){\nreturn cljs.core.str`${(1)}${(2)}${(3)}`;\n}"
although it does inline strings which is probably the most important bit:
cljs.user=> (str (fn [] (js-template str "dude" "dude" 3)))
"function (){\nreturn cljs.core.str`dudedude${(3)}`;\n}"changed my str2 macro to always take a template fn:
cljs.user=> (let [x 1] (str2 str 1 2 x))
"12,1"
cljs.user=> (str (fn [] (let [x 1] (str2 str 1 2 x))))
"function (){\nvar x = (1);\nreturn cljs.core.str`12${x}`;\n}"Maybe a good short name: strt (string template)
yeah definitely don't go with the js-template implementation in shadow. dunno why I went the route I went, could definitely just be a macro