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… 😃Ah, that's a really interesting way to think about comment
: a new top-level context. Like a namespace inside a namespace, where you can get the advantage of using all of the other vars which are defined in the parent namespace, but you don't have to worry about anything in the comment
form interfering with the parent namespace. I can dig that. Block scoped variables in ES6 Javascript is the parallel that springs to mind right away:
const parentNamespace = () => {
const variable = "Hello from the outer context"
const richComment = () => {
const variable = "Hello from the inner context"
console.log(variable)
}
richComment() // -> "Hello from the inner context"
console.log(variable)
}
parentNamespace() // -> "Hello from the outer context"
And yeah, I can see the benefit of defining and loading individual vars within a comment
form as opposed to loading the whole comment
form all at once. I'll give that do
thing a try when I've got some setup code that I want to run all at once.
You guys have been invaluable this month for my Clojure growing pains 😅 Thanks so much for all of the tips, it's really improving my workflow
You are most welcome, sir!
About namespacing. Note that this is only a structural thing regarding the text in the file. When you evaluate something in the comment
block it will both be “from” the same namespace as the file and “to” the same namespace. So hey
will be defined in the file’s namespace. Don’t know if you meant anything else, but just in case. 😃
Looking at your ES6 example it seems like my clarification was needed. Haha.
Clojure has global bindings that can be scoped by namespaces, and local bindings that are lexically scoped, much like the ES6 example there.
> 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`.
Just to clarify here, and maybe Peter meant this, but if the whole comment
form is evaluated, the result is nil
and none of the forms inside it are evaluated.
So if you have def
s in there and they haven't been evaluated, and you evaluate the whole comment form, those vars will still not be defined.