What is the expected behavior of a CLJS atom when it is created with an invalid initial state per its validator? For example:
(atom {} :validator (constantly nil))
In JVM Clojure this immediately throws an IllegalStateException. In CLJS it creates the atom normally with the value {}. I thought the CLJS-specific clause in the docstring might explain it:
> If the current state is not acceptable to the new validator, an Error will be thrown and the validator will not be changed.
But no, the validator is set, and the atom above refuses e.g. (swap! assoc :foo :bar).Yeah doesnβt look like it behaves the same. Docs seem to suggest it should.
> β’ Validators work as in Clojure
> Atoms work as in Clojure.
https://clojurescript.org/about/differences#_vars_and_the_global_environment
Creating the atom in Clojure calls a private function: https://github.com/clojure/clojure/blob/b775c8a289339b4a8fb3f1800a017147c53b0b42/src/clj/clojure/core.clj#L2063 which the uses the set-validator! function
Clojurescript uses deftype to create an object with the validator as a field/prop.
If clojurescript used set-validator! instead, it would behave similarly as that function does throw.
https://github.com/clojure/clojurescript/commit/df6761abf0b2b5008956c57b8591fe1b0fa7d91c
Seems like a bug to me, but would need a breaking change to fix it
Yeah, but this is also just a bug, probably worth fixing & calling out as a potentially breaking change - people shouldn't be relying on this behavior given the doc string.
yeah, invalid initial state - I'd be surprised if there's a lot of code that willfully depends on this behavior
Cool, thanks