So if you actually evaluate the whole comment form it doesn't evaluate any of the forms inside it. That's basically the point of it. When you use a comment form as a playground you need to evaluate each form inside it individually. In Calva, alt+enter
if inside a form inside a comment form, will evaluate the top level form inside the comment.
So if you put your cursor in the first def like here: (def |hey "hey")
and hit alt-enter
the whole def
form there will be evaluated, not the outer comment
form.
It follows usual evaluation rules from within the comment form.
So if you wanted to eval all those def
s at once for some reason, you could put them inside a do
Usually you'd be working with individual forms though. Maybe you want to try some code that has var names in it already - instead of using individual defs
you could use one let
.
(let [hey "hey"
wat "wat"]
(str hey wat))
Sounds like you got the hang of it!
Just want to add about the top-level evaluation inside comment
forms. The way it is designed is that the comment
form creates a new top level context. Let’s look at your example, @andyfry01:
(comment
(def hey "hey")
(def wat "wat")
(str hey w|at))
(The |
represents the cursor.) Since the comment
form created a new top level context, what the top-level evaluator “sees” is this:
(def hey "hey")
(def wat "wat")
(str hey w|at)
And since the current one top level form then is Which is then evaluated, and if hey
is not defined, well, you’ve seen what happens.Just want to add about the top-level evaluation inside comment
forms. The way it is designed is that the comment
form creates a new top level context. Let’s look at your example, @andyfry01:
(comment
(def hey "hey")
(def wat "wat")
(str hey w|at))
(The |
represents the cursor.) Since the comment
form created a new top level context, what the top-level evaluator “sees” is this:
(def hey "hey")
(def wat "wat")
(str hey w|at)
And since the current one top level form then is (str hey wat)
that is then evaluated, and if hey
is not defined, well, you’ve seen what happens. You expect the whole contents of the comment
form to be evaluated. I can see why, but the thing is that if we didn’t treat comment
forms special at all what would be evaluated is the whole comment
form (not just it’s content) and that would give you nil
.
Calva tries to make it easy for you to be in complete control of what is evaluated. So inside that Rich comment block you can move between redefining hey
and then evaluate the str
expression. You might not want to evaluate the last mentioned one at the same time (it could be expensive in some way). So, you are in control.
Sometimes we do want to evaluate a bunch of expressions in one go. For this Clojure gives us the do
macro. So
(comment
(do
(def hey "hey")
(def wat "wat")
(str hey w|at)))
I hope I didn’t confuse things now… 😃