In Clojure: an error occurred while making a Java call with a function enabled, but not in REPL

I have the following code:

(defn post [title content timestamp] (let [[innholdet tajm] [(str "<html> <head> <title>" title " :: " blog_title "</title></head> <body><h1>" title "</h1> <br/>" content "<br/><i>posted " (Date. timestamp) "</i> <br/><a href=\"" (str blog_url) "\">[main]</a> | <a href=\"" (str blog_url) "/" timestamp ".html\">[permalink]</a> </body></html>") (str timestamp".html")]] (spit tajm innholdet) ) ) 

I have another function that passes the header, contents, and UNIX timestamp for the post function above. blog_title and blog_url are var defined at the top of the code. I sorted the HTML for aesthetics, in the "real code" it is not. I also do

 (import 'java.util.Date) (use 'clojure.string) 

If I try to execute the code, I get an error

 java.lang.IllegalArgumentException 

If I delete (date. Timestamp), the code runs fine, but I need to have this function (Date.).

Now if in repl i do

 (import 'java.util.Date) 

Then define var with timestamp

 (def timestamp 1278854531000) ;; Just for pushing a var into (Date.) than just the number 

And then I just copy (let) in the REPL and adjust the vars in the source code to just represent different function names instead of values ​​(since these are REPL and vars, the functions and values ​​that are in the source code do not exist) to be as follows:

 (let [[innholdet tajm] [(str "<html> <head> <title>" (str "title") " :: " (str "blog_title") "</title> </head> <body><h1>" (str "title") "</h1><br/>" (str "content") "<br/> <i>posted " (Date. timestamp) "</i><br/> <a href=\"" (str "blog_url") "\">[main]</a> | <a href=\"" (str "blog_url") "/" (str "1278854531000") ".html\">[permalink]</a></body></html>") (str "1278854531000.html")]] (println innholdet tajm)) 

Now REPL gives me:

 <html> <head> <title>title :: blog_title</title> </head> <body> <h1>title</h1><br/>content<br/> <i>posted Sun Jul 11 15:22:11 CEST 2010</i><br/> <a href="blog_url">[main]</a> | <a href="blog_url/1278854531000.html">[permalink]</a> </body></html> 1278854531000.html nil 

Again, everything has been moved so that it would be more convenient to read, in REPL everything will come out in one big line.

The problem is that I can execute the code in REPL and get the value (Date. Timestamp), and it all works, but when I execute it inside the function in my program, I get the above error. I would appreciate it if someone could tell me what I am missing here.

+4
source share
1 answer

Minor change required:

 (defn post [title content timestamp] (let [[innholdet tajm] [(str "<html> <head> <title>" title " :: " blog_title "</title></head> <body><h1>" title "</h1> <br/>" content "<br/><i>posted " (Date. **(Long/parseLong timestamp)**) "</i> <br/><a href=\"" (str blog_url) "\">[main]</a> | <a href=\"" (str blog_url) "/" timestamp ".html\">[permalink]</a> </body></html>") (str timestamp".html")]] (spit tajm innholdet) ) ) 

However, I would recommend that you define the transfer method in timestamp as a long, not a string.

+2
source

Source: https://habr.com/ru/post/1315443/


All Articles