Fork me on GitHub
#beginners
<
2024-05-07
>
Noah Bogart15:05:52

just to double check: clojure code on the classpath isn't loaded until load is called, right? usually done with use or require or ns. is it possible to inspect the code in a file on the classpath before it's loaded?

delaguardo15:05:26

every "file" on the classpath should be accessible as resource. You can just slurp it before calling load . But clojure code is a vague term, it might be a compiled class or real source file.

👍 1
dpsutton15:05:20

if load can find them on the classpath, then so can you with whatever mechanism you like.

user=> (doc find-ns)
-------------------------
clojure.core/find-ns
([sym])
  Returns the namespace named by the symbol or nil if it doesn't exist.
nil
user=> (find-ns 'clojure.core.reducers)
nil
user=> (subs (slurp ( "clojure/core/reducers.clj")) 0 20)
";   Copyright (c) Ri"

dpsutton15:05:39

here i demonstrate that i haven’t loaded the reducers namespace but it’s on the classpath so i can find it by many mechanisms

Noah Bogart15:05:44

cool, thank you

jumar07:05:15

With emacs/cider, you can cider-open-classpath-entry which might be similar to what you are after?

Alexandre EL-KHOURY16:05:45

I am trying to execute a function from my terminal running the command : clj -X main/hello I am getting the error : Wrong number of args (1) passed to: main/hello even tho my function does not take any parameter... Any idea why ?

dpsutton16:05:32

that’s your issue. the -X will pass a single map to your function. if main/hello accepts no arguments, you will get a “wrong number of args (1) passed to main/hello”

Alexandre EL-KHOURY16:05:21

I had another function with 2 parameters, when I tried to call it I also got the same error. Any idea how to properly call it ? Here's how I did it : clj -X main/get-contracts 100 200

dpsutton16:05:09

-X expects to call a function that takes one associative argument

hiredman16:05:24

the error isn't the same

dpsutton16:05:25

(i’m trying to find documentation about this but surprisingly not finding it in the guide or reference on http://clojure.org)

Sam Ferrell16:05:46

theres an example here https://clojure.org/guides/deps_and_cli clojure -X:repl-server :port 51234

hiredman16:05:35

in the first case it says (1) because 0 args are being passed to a function that expects 1, in the second case it will say something like (2) because 1 arg is being passed to a function that expects 2

hiredman16:05:52

no, I am confusing things

Alexandre EL-KHOURY16:05:29

the first says 1 argument passed (expected 0) The other says 1 argument passed (Expected 2)

Alexandre EL-KHOURY16:05:16

I added a parameter to hello and it works with clj -X main/hello

hiredman16:05:18

oh, well I am just confusing myself then, anyway -X always passes a single argument, which is a possibly empty map

Alexandre EL-KHOURY16:05:51

If my function takes 2 arguments how can I do ? I cant find anything in the doc 😕

hiredman16:05:00

you can't with X

Sam Ferrell16:05:23

you can parameterize the map passed clojure -X:my-fn :foo 1 :bar 2

😃 1
hiredman16:05:34

the way -Xfoo 1 2 is handled is a map {1 2} is what gets passed as the argument

hiredman16:05:52

so if a function takes more than 1 argument you cannot call it with -X

Alexandre EL-KHOURY16:05:08

ahhh so I have to change my function so it takes a map i guess

hiredman16:05:25

if you want to use it with -X

Alexandre EL-KHOURY16:05:02

I know -M is for the main, is there any other flag that could help me in my case ?

hiredman16:05:13

you can use -M to call whatever function you want

hiredman16:05:38

ah, but I guess -m only supports a namespace with a -main function, which I always forget, so you would have to use -e or something, which will make the args stuff not as nice

Alexandre EL-KHOURY16:05:50

yea thats what I meant with -M is for the main lol

hiredman16:05:10

-M is for the clojure.main launcher

hiredman16:05:28

-m is a flag for clojure.main

hiredman16:05:01

so properly -M -m whatever

Alex Miller (Clojure team)17:05:22

there is no CLI option to invoke an arbitrary function. you can: • clj -M -m namespace-with-main to invoke a namespace that has a -main [& args] function • clj -X ns/f to invoke a function that takes a map of args • clj -M file.clj to run all top-level expressions in file.clj

Dave17:05:50

Hello everyone, Context: • I am using @vlaaad's cljfx library for a few of my projects, and I have a basic question about finding callers and callees in Clojure code using events. I don't think my question is cljfx specific, though it has examples from it. Main question: • Is there a simple way to identify where this input parameters current-dir are defined/set/modified? ◦ I usually use "Go to definition" in IntelliJ or Emacs, however decouling the code with event messages seems to completely break this flow. Example: I am swapping the state atom with the current directory that the user has selected from a GUI here:

(defmethod event-handler ::display-files [{:keys [current-dir]}]
  (swap! *state assoc :files (util/list-clojure-files current-dir)))
Here is where I define the component
(defn file-list-view [{:keys [files selection current-dir]}]
  {:fx/type fx.ext.list-view/with-selection-props
   :props   {:selection-mode            :multiple
             :selected-items            selection
             :on-selected-items-changed {:event/type ::events/update-selected-files-svg}}
   :desc    {:fx/type      :list-view
             :cell-factory {:fx/cell-type :list-cell
                            :describe     (fn [item]
                                            {:text (util/str->relative-path item current-dir)})}
             :items        files}})
When I do a "Go to definition" it is able to do a basic search for the keyword here, however, it is not able to tell me in any simple way that the atom is set in the ::display-files handler. Screenshot attached to show IntelliJ's go to definition search. Here is the repo if anyone cares to take a look: https://github.com/aeonik/graph/blob/main/src/aeonik/gui/main.clj

rolt07:05:43

If you use namespaced keywords for your atom's attributes you should be able to see usages, but it will make the code more verbose. Otherwise it just comes to structuring the code, keep events that modifies the same data close together, etc.