Fork me on GitHub
Samuel McHugh09:12:00

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


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ý (HolyJak)11:12:41

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


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

❤️ 3

How come DCE didn't work on it?


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

dominicm07:12:57 here's the removal. I think usefulness argument is weak, but I only expect C-level support


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.


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ý (HolyJak)08:12:51

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


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.


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

goog.string.format = [...]