clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
Samuel McHugh 2020-12-01T09:52:00.070600Z

Does anyone have any good reading on why format in cljs is more difficult to achieve as a core function compared to clj?

dominicm 2020-12-05T06:23:09.141900Z

It was there originally. It was removed because dead code elimination can't take place, so it bloats the core library.

❤️ 1
p-himik 2020-12-05T06:33:33.142100Z

How come DCE didn't work on it?

dominicm 2020-12-05T07:20:19.142300Z

Not entirely sure. I happened to stumble across the commit when searching for how to require format.

dominicm 2020-12-05T07:25:57.142700Z

https://github.com/clojure/clojurescript/commit/48c8d0fafc18375876e10caf960a7c7da27ac308 here's the removal. I think usefulness argument is weak, but I only expect C-level support

p-himik 2020-12-05T07:34:57.142900Z

I don't see from the sources (both core.cljs and goog.string.format) how it would avoid DCE, but I haven't tested it. Maybe will, it's interesting. Regarding the usefulness argument - the useful part is already there in goog.string.format, nothing precludes anyone from using it. However, if you create a Clojure-like wrapper for it, people will absolutely expect it to work just as the Clojure's printf. But it cannot, at least not without a significant extra effort.

p-himik 2020-12-05T08:00:56.143100Z

Ah, duh - goog.string.format mutates goog.string, which is always included by cljs.core. That's why it cannot be DCE'd.

Jakub Holý 2020-12-05T08:14:51.143400Z

Could you be so kind and elaborate on this a little? Aren't strings immutable, and how does that break DCE? #curious

p-himik 2020-12-05T08:34:01.143600Z

Strings are immutable. By "mutates goog.string" I meant that it mutates the module. It breaks DCE (and I'm not an expect here, so don't trust me) because GCC cannot determine if that mutation can be safely removed. Anything could later invoke something like const a = 'format'; goog.string[a]. So basically the same reason why a module that uses defmethod and that's required by something cannot be DCE'd.

p-himik 2020-12-05T08:36:43.143800Z

And of course by "mutates the module" I meant this:

goog.require('goog.string');
[...]
goog.string.format = [...]

Jakub Holý 2020-12-05T16:25:52.158100Z

thank you!!!

p-himik 2020-12-01T11:05:04.070700Z

I don't think there's anything to it worth writing an article. Java supports string formatting. JavaScript does not. It just isn't in the language itself.

Jakub Holý 2020-12-01T11:47:41.070900Z

I believe the Closure library has its version of format but it is different from Java and I guess bridging the differences would be too much pain