cljs-dev

borkdude 2025-12-02T13:31:05.316729Z

I'm discussing an issue with @ptaoussanis about macros and making them also work for scittle. Scittle doesn't read :clj branches but he did put his defmacro in :clj branch, which is probably good practice. What's the downside of not wrapping macros in a reader conditional in CLJS? I have a library that just declares a macro top level below. It seems to work fine for CLJ, CLJS, nbb, scittle, etc. Perhaps the only downside is that for non-bootstrap targets you'll also get some garbage at runtime? Maybe CLJS non-bootstrap could just ignore defmacro if that is the only downside? https://github.com/borkdude/deflet/blob/main/src/borkdude/deflet.cljc

dnolen 2025-12-02T13:33:02.310329Z

I don't know if it really matters much dead code elimination and all

πŸ™ 1
borkdude 2025-12-02T13:34:10.299899Z

Do you think it will or won't be eliminated? I can do a test of course. But is that the only problem with leaving them top level without reader conditional?

dnolen 2025-12-02T13:36:14.768739Z

If your code doesn't reference it assume it will be eliminated

πŸ‘ 1
borkdude 2025-12-02T13:44:28.694059Z

how would one (accidentally maybe) reference the runtime macro? only by referencing it in non-call position I guess? assuming one has a refer like this:

(:require-macros [borkdude.deflet :refer [deflet]])

dnolen 2025-12-02T13:50:02.586219Z

that's not how tree-shaking works - you need to actually use it

dnolen 2025-12-02T13:50:17.583659Z

it's irrelevant what you require etc.

dnolen 2025-12-02T13:51:35.754999Z

assuming the thing you're requiring isn't running arbitrary side effects beyond declaring vars

dnolen 2025-12-02T13:53:12.882209Z

most real programs have some entry point - Closure will figure it out from there

borkdude 2025-12-02T13:55:03.444139Z

I understand that :). but if you don't include this:

(:require-macros [borkdude.deflet :refer [deflet]])
and you do write:
(defmacro deflet ...)
and call (deflet ...) you probably end up referencing the runtime macro

borkdude 2025-12-02T13:55:58.154489Z

just trying to figure out where top level macro in cljc can go wrong for users. I know I've had a case of this ten years ago, but maybe I did accidentally use it in a non-call position or so

dnolen 2025-12-02T14:07:52.428139Z

yes you can call deflet and it will be a function

thheller 2025-12-03T07:12:05.117319Z

FWIW in shadow-cljs during regular CLJS compilation it just https://github.com/thheller/shadow-cljs/blob/c454433fbb06087b559f4c75dca58bf904563eba/src/main/shadow/build/compiler.clj#L545-L555 defmacro and doesn't even compile it, so DCE isn't even a question. very minor tweak.

🎸 1
πŸŽ‰ 1
borkdude 2025-12-03T07:20:57.651169Z

That’s great, thanks