cursive icon indicating copy to clipboard operation
cursive copied to clipboard

Cannot resolve local def

Open armic opened this issue 8 years ago • 5 comments

Hello,

If I try to write the following:

(defn foo [] (def x 3) x)

I get a warning from cursive saying x cannot be resolved, even though this code runs fine.

armic avatar Nov 24 '17 08:11 armic

Just want to say that this seems to apply to any def that is not at the "top level" of a namespace.

royalaid avatar Jul 15 '18 18:07 royalaid

Right. Basically, Clojure discourages defining vars in nested scopes, because people (often coming from Scheme) tend to try to use them as local variables. There are some cases where this is acceptable and Cursive tries to support them:

; Closing over a binding
(let [my-val (something)]
  (defn work-with-my-val []
    ...))
; Some macro forms, e.g. clairvoyant
(trace/trace-forms {:tracer trace/default-tracer}
 (defn add [a b]
   (+ a b)))

If you have either of these cases or something similar you'd like to see supported, feel free to let me know here.

cursive-ghost avatar Jul 15 '18 22:07 cursive-ghost

Mostly it has to do with one of the debugging techniques that I use from time to time to capture a value back to the repl for exploring, something along the lines of https://blog.michielborkent.nl/blog/2017/05/25/inline-def-debugging/.

royalaid avatar Jul 16 '18 15:07 royalaid

Clojure discourages defining vars in nested scopes, because people (often coming from Scheme) tend to try to use them as local variables.

Then, it is wrong to say that the variable cannot be resolved. There should be an intention to convert it to let or a warning against using def locally, but not that it can't be resolved when it can.

matj1 avatar Aug 09 '24 13:08 matj1

We use this pattern a lot, at least in our tests, because it makes for good repl debuggability. Please consider supporting its resolution and maybe turn it into its own warning that i can disable?

(deftest foo
  (def bar 1))

Thread on slack: https://clojurians.slack.com/archives/C0744GXCJ/p1738667786396909

Heliosmaster avatar Feb 05 '25 11:02 Heliosmaster