开发者

In Clojure: Error executing a Java call with let inside a function 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 a title, the content and a UNIX timestamp to the function "post" above. blog_title and blog_url is var defined at the top of the code. I have sorted the HTML for aesthetics, in the "real code" this is no the case. I am also doing

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

If I try to execute the code I get an error

java.lang.IllegalArgumentException 

If I remove (Date. timestamp) the code executes perfectly, but I need to have that (Date.) function.

Now, if in REPL I do

(import 'java.util.Date)

Then define a var with a timestamp

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

And then I just copy the (let) into REPL and adjust the vars in the original code to rather just represent the different function names instead of the values (since this is the REPL and the vars, functions and values that are in the orig开发者_如何学编程inal code doesn't exist), to be like this:

(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 the 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 shifted so that it would be more friendly to read, in REPL everything comes out in one large string.

The problem here is that I can execute the code in REPL and get the value of (Date. timestamp) and everything works out, but when I execute it inside of a function in my program I get the above mentioned error. Would appreciate if anyone could tell me what I am missing here.


Just a small change is necessary:

(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 still recommend that you figure out a way to pass in the timestamp as a long instead of a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜