This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-03-29
Channels
- # announcements (1)
- # biff (5)
- # calva (10)
- # cljdoc (4)
- # clojure (17)
- # clojure-europe (15)
- # clojure-norway (38)
- # clojurescript (53)
- # community-development (1)
- # cursive (2)
- # datomic (14)
- # fulcro (11)
- # funcool (1)
- # hyperfiddle (7)
- # introduce-yourself (2)
- # jobs-discuss (17)
- # missionary (7)
- # releases (4)
- # shadow-cljs (30)
- # slack-help (6)
- # specter (5)
- # squint (22)
Am I supposed to be able to do (require '[something.something :as something])
and then use the alias? I’m doing it here https://github.com/PEZ/guardrails-malli-mini/blob/e5a3a45f92d38eba5bb93a0f410023aaf3b28251/src/main/guarded.cljc#L35-L39 and only the fully qualified access works…
Thomas will probably correct or expand on my answer once he sees this. :) But IIRC a dynamic require
is really only a REPL thing. It can't work in a file because the compiler needs to know what m
is, but it doesn't before actually running the code that require
generates.
This is in a Rich comment form though, and thus in the REPL. I first evaluate the require, then fail to use the alias in the following evaluations.
Erm. That does surprise me.
What also surprises me is that *ns*
is nil
, at least in a shadow-cljs REPL.
OK, fixed. *ns*
is still nil
though.
Given this code:
(comment
(require '[clojure.string :as str])
(str "a" "b")
)
I attached to a shadow-cljs REPL, sent the first form there and then sent the second form there:
(require '[clojure.string :as str])
=> nil
(str "a" "b")
=> "ab"
So that seems to have worked just fine.Yo, sleep deprivation is no joke. The code above is the dumbest one I've written in a long time. At least it took me only a minute to see that.
Yeah, can reproduce what you see, so neither or us is doing anything wrong (or we both are):
(str/join ["a" "b"])
------ WARNING - :undeclared-ns ------------------------------------------------
Resource: <eval>:1:2
No such namespace: str, could not locate str.cljs, str.cljc, or JavaScript source providing "str"
--------------------------------------------------------------------------------
------ WARNING - :undeclared-var -----------------------------------------------
Resource: <eval>:1:2
Use of undeclared Var str/join
--------------------------------------------------------------------------------
There was a similar thread a year ago without any resolution: https://clojurians.slack.com/archives/C03S1L9DN/p1678325483467779
On my end, I'm attaching to shadow-cljs via nREPL and then run (shadow/repl build-id)
to get to the CLJS REPL.
Using the REPL via npx shadow-cljs cljs-repl build-id
works just fine:
shadow-cljs - connected to server
cljs.user=> 1
1
cljs.user=> (require '[clojure.string :as str])
nil
cljs.user=> (str/join ["a" "b"])
"ab"
@U05224H0W Any clue why aliases work when the REPL is started via cljs-repl
but don't work if an nREPL connection is used?
It worked for me as well with the quickstart-browser
project.
I'm testing whether switching to deps.edn
changes it, because that's what I was using for a test in my own project.
I suspect I just broke something. REPL require has been broken so many times I can't even count it anymore 😛
ie. you do something at the REPL and a triggered hot-reload deletes everything you did
or you do something at the REPL that then causes a circular relationship between namespaces and breaks hot-reload 😛
it worked on the first try, but out of habit I did a load-file and switched to the ns first
Huh. I just updated to 2.28.2 (was using 2.27.5 before) and it now works for me just fine. Tried a few namespaces in the same REPL session, tried restarting the REPL session - all works. @U0ETXRFEW What version do you have?
Ah, it works only when run directly via the nREPL window. Doesn't work when I use a comment
form.
> Ah, it works only when run directly via the nREPL window. Doesn’t work when I use a comment
form.
That’s extra weird. Maybe the nREPL window in Cursive is using user
or something as ns and that makes it dodge the problem.
require
uses the ns of the current REPL "session", which if you just eval something and don't set it manually is cljs.user
so in case of cursive the eval that arrives is
{:eof? false, :ns demo.browser, :form sg/query-ident, :source "sg/query-ident"}
(note that I'm testing in shadow-cljs project itself, doesn't really matter which ns is used)
but as you can see cursive supplied the :ns demo.browser
here, although I never actually switched to it
so the alias exists in cljs.user
due to the require executing there, but not the ns you actually eval in
Interesting, Calva used to do in-ns
as part of any evaluation. But I changed that because sending the ns
parameter along with the nREPL eval
op should be quite equivalent. Not equivalent enough then.
Hello, i am pretty new, and this question is maybe stupid, but if i want to consume an api with clojurescript do i use a special library or just interop with javascript using fetch, what is the recommended way?
It sort of depends. If you are just using clojurescript, then interop is fine. If you are using re-frame, then you'll want to hook into it's capabilities. And that would be similar for any other framework/library you might use.
I personally use https://github.com/oliyh/martian since the backend at my job exports OpenAPI 3.0 schemas
Fetch is fine.
thanks all for the answers, i will take a look into martian also