cursive-ide / cursive

Cursive: The IDE for beautiful Clojure code
585 stars 7 forks source link

Cannot resolve local def #1901

Open armic opened 6 years ago

armic commented 6 years ago

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.

royalaid commented 6 years ago

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

cursive-ide commented 6 years ago

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.

royalaid commented 6 years ago

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/.

matj1 commented 2 months ago

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.