Home > Net >  Why clojure.repl/source does not works for my custom functtion
Why clojure.repl/source does not works for my custom functtion

Time:03-15

When I execute the clojure.repl/source activate in repl, it gives me the source of the activate function.

Now, I defined a custom function in namespace tutorial.test

(ns tutorial.test)
    (defn return_type_of_arg
        [x]
        (type x)
)

After switching the repl namespace to tutorial.test and loading this file in repl, I tried to execute clojure.repl/source return_type_of_arg.

It's giving me the output as Source not found

What's going wrong?

EDITS:

I have confirmed that the namespace is shifted to the one required. Please see the following image:

enter image description here

These are the exact steps I followed:

  1. Created a file test.clj and added above fucntion into it.
  2. Left clicked this file in Intellij Idea and selected option Switch REPL namespace to current file.
  3. Left clicked this file in Intellij Idea and selected option Load file in REPL.

CodePudding user response:

Below is a transcript of a REPL session that shows the steps to make this work.

dorabs-imac:tmp dorab$ cat tutorial.clj
(ns tutorial)

(defn return_type_of_arg
  [x]
  (type x))
dorabs-imac:tmp dorab$ clj -Sdeps '{:paths ["."]}'
Clojure 1.10.3
user=> (require 'tutorial)
nil
user=> (in-ns 'tutorial)
#object[clojure.lang.Namespace 0x187eb9a8 "tutorial"]
tutorial=> (clojure.repl/source return_type_of_arg)
(defn return_type_of_arg
  [x]
  (type x))
nil
tutorial=> 

dorabs-imac:tmp dorab$ 

Added in edit:

  1. First I created a file called tutorial.clj with the ns form and the defn of the function.
  2. Then I started a REPL with the current directory (.) in the CLASSPATH so Clojure knows where to load the file from.
  3. Then I loaded up the file using require. This loads the function definition into the tutorial namespace.
  4. Then I changed the current namespace of the REPL to be tutorial using the in-ns function.
  5. Then I ran the clojure.repl/source function.

Further edit:

I think I have identified the problem you are facing. It seems that the way you are using IntelliJ, it is sending the contents of the file to the REPL, rather than requireing the file. According to the docs for source, "Prints the source code for the given symbol, if it can find it. This requires that the symbol resolve to a Var defined in a namespace for which the .clj is in the classpath." [Emphasis mine]. So, when the file contents are sent to the REPL directly (without using require) there is no .clj file for source to look for the source. The following transcript demonstrates that. Compare the following transcript with the transcript above that does work.

dorabs-imac:tmp dorab$ clj
Clojure 1.10.3
user=> (ns tutorial)

(defn return_type_of_arg
  [x]
  (type x))

nil
tutorial=> tutorial=> #'tutorial/return_type_of_arg
tutorial=> tutorial=> (clojure.repl/source return_type_of_arg)
Source not found
nil
tutorial=> 
  • Related