Comments (11)
Yes, Array.prototype.join
calls .toString
and not valueOf
from squint.
So there's another option here: string interpolation using template literals.
Note that there's a mild difference between the two syntaxes. Addition would coerce the expression to a primitive, which calls valueOf() in priority; on the other hand, template literal would coerce the expression to a string, which calls toString() in priority. If the expression has a @@toPrimitive method, string concatenation calls it with "default" as hint, while template literals use "string". This is important for objects that have different string and primitive representations — such as Temporal, whose valueOf() method throws.
It seems like we'd want string interpolation since it more closely maps to Clojure's str?
from squint.
I wrote some code to see the differences between the two options: https://gist.github.com/corasaurus-hex/2c03d8abc1c8827b44b59fbdc31a3622
I can't see any difference between them in practice except the ones noted in the paragraph I quoted.
So do we want the behavior of string concatenation or string interpolation? I'd vote for interpolation, both for forwards compatibility and it being the least surprising behavior.
from squint.
@corasaurus-hex String interpolation is a different issue: this issue is just about generating more efficient code for (str foo bar)
=> cherry/str(foo,bar)
vs foo + bar
.
I think string interpolation should also be a built-in feature that compiles directly to JS string interpolation. We can support this using a reader template #i "foo ${name}"
. I'll make a different issue for that.
from squint.
The generated code for (str foo bar) can either be "" + foo + bar or it can be `${foo}${bar}` and I had intended to examine which was the better choice.
from squint.
from squint.
The main reasons, for me:
- template interpolation calls
.toString
on the elements vs+
which calls.valueOf
on all the elements. I think that more matches with the spirit ofstr
. - following from that, calling
.toString
on the elements makes it forwards compatible with Temporal. - cljs calls
.toString
on all the elements, too.
String interpolation via templates is just a means to an end for me, though, what I really care about is using .toString
instead of .valueOf
and just doing "" + ...
will call .valueOf
.
from squint.
Right. How CLJS compiles (str "foo" "bar")
is:
["foo","bar"].join('');
Does that match the .toString
behavior? If so, then we could emit that too.
from squint.
But then again, we could just handle that in the str
function instead instead of inlining this, probably won't matter much.
from squint.
I don't have a strong preference. My hunch is that my usage of str
would go down in application code if there was a reader conditional for doing string interpolation as in #32.
So instead of doing something like
(str "Hello, " name "!")
I would do
#i "Hello, ${name}!"
The only time I really need str then is for the (apply str ,,,)
case I noted in the OP, which can't be expanded to infix anyway. So I think based on that we don't need to macroify it if we go with #32
from squint.
Agreed
from squint.
Related Issues (20)
- Improvement: use arrow functions instead of "function" for implicit iife
- optimize function definitions for esbuild HOT 6
- `squint/core` Shuffle function suggestions HOT 2
- `squint/core` Prefer slice over pop HOT 4
- cljs is not defined when using `case` HOT 3
- Squint remove ":" for keyword value HOT 16
- class definitions not showing in repl HOT 14
- `nil` keys in map HOT 3
- zipmaps are disordered HOT 3
- `nil` causes a crash in html rendering HOT 1
- html renderer crashes when rendering nil from variable
- How to emit try catch? HOT 3
- REPL fails with `(extends class-1)` in defclass HOT 10
- `html` spread `nil` HOT 1
- Support `js$console` for requiring global libs
- (:query req) or (get req :query) not working on express request object
- could squint be used as a clojurescript compiler to javascript, output of which works with Scittle? HOT 3
- Macros get resolved as normal functions HOT 1
- HTML is not escaped in dynamic expression
- logical not is not working
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from squint.