plumatic / schema

Clojure(Script) library for declarative data description and validation
Other
2.4k stars 256 forks source link

defrecord doesn't work with pprint #322

Closed ryang-bgl closed 8 years ago

ryang-bgl commented 8 years ago

Hi, I am using Emacs Org Babel with the following codes:

+begin_src clojure :results pp

(require '[schema.core :as s]) (s/defrecord MyDomainModel [date :- Long ])

+end_src

The above codes were expanded to: (clojure.pprint/pprint (do (require '[schema.core :as s]) (s/defrecord MyDomainModel [date :- Long])))

Running the above codes in repl got: Unable to resolve symbol: MyDomainModel in this context

running the above codes without pprint is fine. Not sure if it is an issue of pprint or the defrecord macro. Did a marcoexpand-all, found very hard to read and figure out what's wrong.

Appreciate if someone could help to have a look. Cheers, Rui

w01fe commented 8 years ago

This isn't a schema issue. Without invoking Schema:

user> (do (defrecord Foo []) Foo)
user.Foo
user> (clojure.pprint/pprint (do (defrecord Foo2 []) Foo2))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: Foo2 in this context, compiling:(/private/var/folders/_g/_nhxhpvn249c7ylfjwftblvr0000gp/T/form-init2087833310089175503.clj:1:24) 

Clojure compiles each top-level form as a unit, with a special case exception for do (which it breaks into separate compilation units). When you wrap multiple forms in a pprint, the whole thing is treated as a single unit, and the class defined by defrecord is not visible in subsequent forms.

ryang-bgl commented 8 years ago

Thanks for looking at this. This is not a schema issue. I'll close it.