Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defonce overwritten in advanced mode #1185

Open
danjohansson opened this issue Jun 11, 2024 · 3 comments
Open

defonce overwritten in advanced mode #1185

danjohansson opened this issue Jun 11, 2024 · 3 comments

Comments

@danjohansson
Copy link

The following code gives the printout x: 2
Expected printout x: 1

(ns test)

(defn run-defonce [v] (defonce x v))
(run-defonce 1)
(run-defonce 2)

(println "x: " x)

I have tried the same code in the clojure clojurescript compiler, with the advanced mode setting turned on, without being able to reproduce.

@thheller
Copy link
Owner

It is correct that shadow-cljs treats defonce just like def for release builds, as generally defonce only exists as a REPL aid so that reloading a namespace does not overwrite state you might want to keep.

There is currently no flag to turn this off. I could add one, but would rather get you away from doing something you are not supposed to be doing. def, defonce, defn are all supposed to be top level forms. Embeeding a defonce inside a defn is nightmare fuel.

Why are you doing this in the first place?

@danjohansson
Copy link
Author

Ok thanks for clarifying.
I was surprised that it worked the way it did and not according to the Clojure documentation.
I guess this would be the main argument for changing this.

We use it during initialization as a way of making sure some code only run once.
Knowing that the pattern is problematic in general, I thought this would be safe. Guess I was wrong! 😄

@thheller
Copy link
Owner

well, if you def something that only runs once as a namespace is only ever loaded once in release builds.

So, (defonce x (only-ever-runs-once 1)) works just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants