This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-21
Channels
- # announcements (1)
- # aws (18)
- # babashka (5)
- # beginners (72)
- # biff (2)
- # calva (38)
- # cider (2)
- # clj-commons (6)
- # clj-yaml (2)
- # clojars (7)
- # clojure (41)
- # clojure-austin (5)
- # clojure-europe (78)
- # clojure-nl (1)
- # clojure-norway (18)
- # clojure-uk (3)
- # clojurescript (13)
- # component (9)
- # cursive (37)
- # datahike (3)
- # datomic (7)
- # fulcro (7)
- # graphql (3)
- # holy-lambda (2)
- # honeysql (8)
- # introduce-yourself (1)
- # jobs (1)
- # kaocha (1)
- # leiningen (19)
- # lsp (104)
- # malli (5)
- # nbb (8)
- # off-topic (60)
- # polylith (22)
- # portal (2)
- # reagent (24)
- # reveal (1)
- # shadow-cljs (126)
- # test-check (11)
- # tools-build (39)
- # vim (23)
- # xtdb (10)
Got a REPL question. I have anonymous functions defined in a let binding. When I try to send a piece of code inside the let binding to the repl it tells me that the var is undefined. I've used "Load File in REPL". Is there a way to use the repl with let forms? The code
(let [get-tile-values (fn [m n] (str (get-in m [:resources n]) " " (get-in m [:rolls n])))
get-tile-css (fn [m n] (color-classes (str (get-in m [:resources n]))))]
[:div.grid.grid-cols-5.gap-1
[:div.text-white.text-center.p-2 (get-tile-values m 1)
{:style {:class (get-tile-css m 1)}}]
;...
When I try to use the send X to REPL
command it says it can't find the var. For example send (get-tile-values m 1) to REPL
results in a "Use of undeclared Var front-end.views/get-tile-values" error messageTried that and got the same error. I used send (defn map->grid) to repl
. Running the functions declared inside the let binding gives the same error
if there is no namespace-wide binding of get-tile-values
then when you send (get-tile-values m 1)
, clojure won't know what you mean by get-tile-values
I manually loaded m
into repl from a comment
sending the let form still doesn't let me use the methods declared inside the let
I can declare the functions globally, but that's a workaround
> sending the let form still doesn't let me use the methods declared inside the let you mean you send the let form and it errors out?
the second. I can send the let form fine, but when I call the (get-tile-values m 1) it errors
I'm using the send at cursor function. Even though my cursor is inside the let form, it has no knowledge of the let form's binding?
(let [get-tile-values (fn [m n] (str (get-in m [:resources n]) " " (get-in m [:rolls n])))
get-tile-css (fn [m n] (color-classes (str (get-in m [:resources n]))))]
[:div.grid.grid-cols-5.gap-1
[:div.text-white.text-center.p-2 (get-tile-values m 1)
{:style {:class (get-tile-css m 1)}}]
but when from inside the let form you send (get-tile-values...)
then it will literally send only that
dang. What's the best way to use the repl for testing anonymous functions in let bindings? The only thing I can think of is declare them globally during development and move them into let for production
you can comment out the body of the let form and only make the body the form you want to evaluate in the scope of that let form
Appreciate it. Thanks!
Super helpful. Thanks a lot 🙂
@U01ES0TF6V6 I've used this macro in the past for exactly this problem. In the repl you just make sure you have switched to the namespace of your editor, type (deflet [])
and cut and paste the let bindings you want to "inline def" as its first argument (i.e. replacing the empty vector.) The macro will inline def them all for you so you can get on with the repling. Much quicker for large let blocks. https://gist.github.com/henryw374/5dfcbf9d27ec89b360058e81339cbe4e
When inline defing a lot it can be useful to "zap" the namespace a la https://clojurians.slack.com/archives/C0744GXCJ/p1667378949616179 :)
@U3TSNPRT9 with deflet, could it also work if I changed let
to deflet
in the function? And then I could change it back to let
when I was done messing with it?
What's the advantage of zapping? I can reload the file and I can def the vars a second or third time. I tend to throw things in comment
blocks so I can quickly reload useful snippets.
Changing let to deflet might indeed work - never tried that! If you reload it won't "forget" anything previously "def'd". That's why to really clean up you have to "zap" or restart the repl. Reloading also doesn't drop other things like aliases you previously defined in your ns requires. Swapping aliases in your requires can similarly leave you unable to reload completely.