开发者

Why can't I reference result of (defrecord Foo [bar]) in REPL after (load-file "foofile.clj")?

开发者 https://www.devze.com 2023-03-03 20:51 出处:网络
My file looks like 开发者_开发问答this, more or less: (ns foofile) (defprotocol foo-protocol \"foo(lish example)\"

My file looks like 开发者_开发问答this, more or less:

(ns foofile)

(defprotocol foo-protocol 
  "foo(lish example)"
  (foo-method [this] "foo docs")

(defrecord Foo [biz])

(def foo (Foo. "baz"))

I start the REPL and type

(load-file "foofile.clj")

and it prints

#'foofile/foo

Now I can reference foofile/foo and foofile/foo-protocol, but when I try foofile/Foo, I get

java.lang.Exception: No such var: foofile/Foo (NO_SOURCE_FILE:0)

The same (or substantially similar) code works just dandy if I manually enter in into the REPL (specifically, I followed this blog post). What's going on here? What's different between doing this in the REPL and loading a file?


You need to import Foo, since it's a java class:

(import 'foofile.Foo)

The reason it works when you enter your code in a repl is that you're in the same namespace you declared Foo in when you're referring to Foo (probably the namespace user).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号