This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-03-15
Channels
- # babashka (4)
- # beginners (136)
- # calva (63)
- # clerk (7)
- # clj-kondo (8)
- # clojure (43)
- # clojure-boston (1)
- # clojure-europe (37)
- # clojure-nl (1)
- # clojure-norway (11)
- # clojure-uk (3)
- # clojurescript (6)
- # clr (1)
- # code-reviews (16)
- # cursive (45)
- # datomic (2)
- # docker (32)
- # emacs (10)
- # events (2)
- # exercism (1)
- # fulcro (3)
- # hugsql (1)
- # hyperfiddle (47)
- # leiningen (3)
- # lsp (30)
- # malli (39)
- # missionary (1)
- # off-topic (24)
- # pathom (2)
- # portal (14)
- # practicalli (5)
- # rdf (13)
- # reagent (18)
- # reitit (18)
- # releases (7)
- # remote-jobs (1)
- # sci (2)
- # shadow-cljs (45)
- # sql (7)
- # tools-build (11)
- # xtdb (13)
Not sure if it's a Cursive or IntelliJ problem: sometimes ^{:deprecated "why"}
are not rendered as deprecated (using strikethrough) for example. In some ns'es it's ok, in others it's ignored.
i found this deprecation meta-data handling a great feature too!
i haven't used it much with a reason value though.
we only have 2 deprecated namespaces in our main code-base at the moment, but 15 deprecated functions and 2 deprecated tests too.
the :deprecated
markers also serve as technical debt indicator too.
I like using it during refactorings, to have a better perspective on what's outdated
I just checked the code, and as far as I can tell this should be working. I’d be interested in a repro case.
Is there any sort of “reformat selection” capability in cursive? If not, I would so love this as a feature.
Ah, there is! Opt+Cmd+L
https://clojurescript.org/about/differences#_namespaces > :require supports :as, :refer, and :rename … > :rename specifies a map from referred var names to different symbols (and can be used to prevent clashes)
Seems to be an issue another user just reported https://github.com/cursive-ide/cursive/issues/2780
This code works fine for me, no issues:
(ns test-rename
(:require [clojure.string :refer [join] :rename {join str+}]))
(comment
(str+ "-" ["a" "b"])
)
To be clear - we’re talking about Cursive supporting the semantics of rename, not the functionality of the code.
@U0HJNJWJH thanks for looking into my ticket!
indeed my sample code was syntactically incorrect; my bad.
i was too confident in my ability to type Clojure code up, without any syntax highlighting or paren balancing, right into the GitHub issue's <textarea>
I've just tried what's the behaviour in Cursive 1.12.8-eap4-2023.1
and it seems slightly different from what I remembered, but still a bit problematic, if
1. Cmd-/
Comment with Line Comment the (str+ "-" ["a" "b"])
line
2. :refer [join]
becomes underlined as an Unused refers
warning
3. Optimize imports removes the :refer [join]
4. uncomment (str+ "-" ["a" "b"])
5. program broken after REPL restart
nREPL server started on port 62035 on host localhost -
Loading dev/xxx.clj... done
(str+ "-" ["a" "b"])
Syntax error compiling at (dev/xxx.clj:7:3).
Unable to resolve symbol: str+ in this context
However, if I just use the #_
comment reader syntax, this does NOT happen!
What I was trying to say, is that I would consider the mention of a symbol in the :rename
clause a usage of that symbol, so that symbol wouldn't be removed, even if it's not used anywhere else in that scope.I’ll look at onetom’s case soon. @U083D6HK9, are you seeing this or a different problem? :rename was broken for quite some time, but I fixed it a while back.
Oh, that’s what I get for reading quickly. No, I think my problem is different — it’s not resolving them at all. See attached screenshot.
Hmm. It could be added, I suppose. This is in an existing codebase that’s working with the above code. Reading the CLJS doc carefully does make me think the existing code is incorrect and happens to work correctly.
well, for me this code without :refer just does not work
(ns test-rename
(:require [clojure.string :rename {join str+}]))
(str+ "-" ["a" "b"])
I agree! I like to use aliases for all the things. The existing codebase has this convention, so I will follow it. I also asked in #clojurescript and it would seem this works by happenstance https://clojurians.slack.com/archives/C03S1L9DN/p1679147048145799
@U0HJNJWJH the problem with not using :rename
, but a different var — eg. (def e-db rf/reg-event-db)
—, is that we are losing access to metadata, like :arglists
or :doc
:
(ns xxx
(:require [clojure.string :as str :refer [join] :rename {join str+}]))
=> nil
(def str-join str/join)
=> #'xxx/str-join
(clojure.repl/doc str+)
-------------------------
clojure.string/join
([coll] [separator coll])
Returns a string of all elements in coll, as returned by (seq coll),
separated by an optional separator.
=> nil
(clojure.repl/doc str-join)
-------------------------
xxx/str-join
=> nil
which means losing F1
help in Cursive too.
So :rename
is very much a useful built-in capability, which can help with
1. making code more concise
2. resolving naming conflicts
3. adjusting a confusing name of some code in a library
while maintaining identical behaviour as using the original var.using str/join
is just fine, nothing more is really needed.
personally I prefer to always use library vars with namespace.
it's not about str/join
specifically; that's just an example, which anyone should understand easily.
an actual use-case is more like this:
[gini.rmap :as rmap :refer [rmap ref] :rename {ref $}]
while :rename
is a rarely needed capability, we have this line in our code-base literally 69 times.
it means, that the Optimize imports... action has 69 potential namespaces to break our code...
and using (rmap/ref :some-key)
instead ($ :some-key)
makes a lot of difference in readability, when there are a lot of occurrences of $
s.(rmap
{:system (str (gensym "in-mem-sys-"))
:client (d/client (-> ($ :cfg) (assoc :system ($ :system))))
:db-name (str (gensym "db-"))
:db-ref (ensure-db! ($ :client) ($ :db-name))
:conn (d/connect ($ :client) ($ :db-ref))
:val (d/db ($ :conn))
:branch (d/with-db ($ :conn))
:txrs (ensure-txs! ($ :conn) ($ :txs))})
vs
(rmap/rmap
{:system (str (gensym "in-mem-sys-"))
:client (d/client (-> (rmap/ref :cfg) (assoc :system (rmap/ref :system))))
:db-name (str (gensym "db-"))
:db-ref (ensure-db! (rmap/ref :client) (rmap/ref :db-name))
:conn (d/connect (rmap/ref :client) (rmap/ref :db-ref))
:val (d/db (rmap/ref :conn))
:branch (d/with-db (rmap/ref :conn))
:txrs (ensure-txs! (rmap/ref :conn) (rmap/ref :txs))})
rmap/rmap
is fine, no guesses where rmap
symbol is coming from (the code above or another ns)
again: for me rmap/rmap
is more readable 🙂
(rmap
{:system (str (gensym "in-mem-sys-"))
:client (d/client (-> ($ :cfg) (assoc :system ($ :system))))
:db-name (str (gensym "db-"))
:db-ref (ensure-db! ($ :client) ($ :db-name))
:conn (d/connect ($ :client) ($ :db-ref))
:val (d/db ($ :conn))
:branch (d/with-db ($ :conn))
:txrs (ensure-txs! ($ :conn) ($ :txs))})
vs
(rmap/rmap
{:system (str (gensym "in-mem-sys-"))
:client (d/client (-> (rmap/ref :cfg) (assoc :system (rmap/ref :system))))
:db-name (str (gensym "db-"))
:db-ref (ensure-db! (rmap/ref :client) (rmap/ref :db-name))
:conn (d/connect (rmap/ref :client) (rmap/ref :db-ref))
:val (d/db (rmap/ref :conn))
:branch (d/with-db (rmap/ref :conn))
:txrs (ensure-txs! (rmap/ref :conn) (rmap/ref :txs))})
second I can read without looking in ns declaration
for first I have no clue what exactly goes on> u must be a java programmer, if u r fine with that kind of superfluous repetition
I write Clojure too long to value explicit code with ns aliases. That's why I stopped using :refer to avoid any ambiguity. At the moment I only :refer deftest
and testing
in tests :-P
you can see my code without using :refer here https://github.com/strojure
i prefer NS aliases too, but there are cases where such verbosity negatively affects readability.
these asd/asd
style names i can't really understand though; but that's not a :rename
question, but a :refer
or not to :refer
question 🙂
for the record, the rmap
in my examples is this lib:
https://github.com/aroemers/rmap
Tried adding :refer
to a :require
with a :rename
and it seems Cursive does not recognize the renamed vars still.
@U083D6HK9 maybe it's a cljs specific issue?
i was also wondering if those vars are actually macros, but as i see in the source code, they are not:
https://github.com/day8/re-frame/blob/master/src/re_frame/core.cljc
what do they mean by #!clj
though, in the docstring of reg-event-db
?
Example Usage:
#!clj
(reg-event-db
:token
(fn [db event]
(assoc db :some-key (get event 2))) ;; return updated db
did u take that screenshot of the problematic NS clause from a .cljc
or a .cljs
file?
(im just shooting in the dark here)