Does anyone have any good reading on why format
in cljs is more difficult to achieve as a core function compared to clj?
It was there originally. It was removed because dead code elimination can't take place, so it bloats the core library.
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.
https://github.com/clojure/clojurescript/commit/8f518760a3df8b351208e97bb70270856623bb0a here it was added
https://github.com/clojure/clojurescript/commit/48c8d0fafc18375876e10caf960a7c7da27ac308 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.
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.require('goog.string');
[...]
goog.string.format = [...]
thank you!!!
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.
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