Coder Social home page Coder Social logo

Macroify: str about squint HOT 11 CLOSED

lilactown avatar lilactown commented on June 19, 2024
Macroify: str

from squint.

Comments (11)

corasaurus-hex avatar corasaurus-hex commented on June 19, 2024 1

Yes, Array.prototype.join calls .toString and not valueOf

from squint.

corasaurus-hex avatar corasaurus-hex commented on June 19, 2024

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.

corasaurus-hex avatar corasaurus-hex commented on June 19, 2024

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.

borkdude avatar borkdude commented on June 19, 2024

@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.

corasaurus-hex avatar corasaurus-hex commented on June 19, 2024

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.

borkdude avatar borkdude commented on June 19, 2024

from squint.

corasaurus-hex avatar corasaurus-hex commented on June 19, 2024

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 of str.
  • 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.

borkdude avatar borkdude commented on June 19, 2024

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.

borkdude avatar borkdude commented on June 19, 2024

But then again, we could just handle that in the str function instead instead of inlining this, probably won't matter much.

from squint.

lilactown avatar lilactown commented on June 19, 2024

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.

borkdude avatar borkdude commented on June 19, 2024

Agreed

from squint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.