@andyfry01 Love to see your use of the comment form! It really is like a notebook where you can just try things out, come back to it later, and build things up.
Inside the comment forms in Calva, you can have your cursor anywhere inside a form and hit alt+enter
(but the Mac equivalent) to evaluate, so no need to put your cursor at the beginning/end of the form to evaluate (not sure if that was done in the latest stream).
Another tip - you can refactor this use of swap!
to be something like (swap! app-state random-food 0)
if you switch the args around to random-food
.
alt+enter
on Mac as well.
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.
Oh, I thought alt
was something else on Mac, is it not?
The function passed to swap!
will receive the value of the atom as its first param
It is sometimes/often labeled option
, but it is aka alt
.
This has confused some Mac users, because they do not find the alt
key on their keyboards. So it is good to be aware of.
Also, that issue with Calva asking for the nrepl port at jack-in should be fixed soon
Yeah. I just merged the the PR: https://github.com/BetterThanTomorrow/calva/pull/972
@brandon.ringe The comment form has been revolutionary for me! I've got a few of them already in the file I was working in today. You did indeed guess it: they're all laid out like little scratch pads where I can define a bunch of variables and functions and experiment around with them, and I can isolate different ideas inside the scopes of each comment
.
I've been a mac user for ... a while, and to this day I get mixed up with what keys are to be found on which keyboards š
It gets especially weird when apps start swapping around control
with command
I was actually trying out option+enter
(windows equivalent: alt+enter
earlier today. I was trying to use that to load the whole comment
block, vars and all, into the REPL.
There may be a bug, or maybe I don't understand the command fully, but if I try it with this code for example, I get Unable to resolve symbol: hey in this context
(comment
(def hey "hey")
(def wat "wat")
(str hey wat))
Thus requiring me to go through each form, load it into the REPL, and then evaluate (str hey wat)
at the end.