Myrkvi
Myrkvi

Reputation: 131

Clojure - "Exception in thread "main" java.lang.ClassCastException: seesaw.core.proxy"

I'm currently learning Clojure, and I haven't worked with any Lisp dialect before.
So, trying to make a simple GUI test, I get a ClassCastException in seesaw.

core.clj file:

(ns veg-gui.core)
(use 'seesaw.core)

(def f (frame :title "Hello world!") )


(defn -main [& args] 
  ( (config! f :content "Hello world.")
    (-> f pack! show!) ) )

The full error.

Exception in thread "main" java.lang.ClassCastException: seesaw.core.proxy$javax
.swing.JFrame$Tag$a79ba523 cannot be cast to clojure.lang.IFn
        at veg_gui.core$_main.doInvoke(core.clj:8)
        at clojure.lang.RestFn.invoke(RestFn.java:397)
        at clojure.lang.Var.invoke(Var.java:411)
        at user$eval5181.invoke(NO_SOURCE_FILE:1)
        at clojure.lang.Compiler.eval(Compiler.java:6511)
        at clojure.lang.Compiler.eval(Compiler.java:6501)
        at clojure.lang.Compiler.eval(Compiler.java:6477)
        at clojure.core$eval.invoke(core.clj:2797)
        at clojure.main$eval_opt.invoke(main.clj:297)
        at clojure.main$initialize.invoke(main.clj:316)
        at clojure.main$null_opt.invoke(main.clj:349)
        at clojure.main$main.doInvoke(main.clj:427)
        at clojure.lang.RestFn.invoke(RestFn.java:421)
        at clojure.lang.Var.invoke(Var.java:419)
        at clojure.lang.AFn.applyToHelper(AFn.java:163)
        at clojure.lang.Var.applyTo(Var.java:532)
        at clojure.main.main(main.java:37)

(Also, I'm running this with lein run

Upvotes: 2

Views: 676

Answers (2)

ffriend
ffriend

Reputation: 28492

Seesaw is not included into Clojure core, so, as in any other programming language, you need to include this library into your project. With Leiningen it's pretty easy - just add [seesaw "x.y.z"] (where "x.y.z" is the version) to your project.clj. More details and examples on project home page.

UPD. I payed more attention to your exception and your code. You have an error at line:

( (config! f :content "Hello world.")

Unlike most other languages, in Lisp brackets play more important role than just grouping arguments. When you write:

(foo ...)

foo is expected to be function (instance of IFn). Thus, Clojure tries to treat expression (config! f :content "Hello world.") as function, which is not true, so exception is thrown. To fix it just remove additional brackets:

(defn -main [& args] 
  (config! f :content "Hello world.")
  (-> f pack! show!))

Note, however, that writing expressions sequentially in Clojure is not always possible. Normally you have to use do to perform several actions one after another. Fortunately, defn includes do implicitly, so you don't need explicit one.

Upvotes: 0

kotarak
kotarak

Reputation: 17299

(defn -main [& args] 
  ( (config! f :content "Hello world.")
    (-> f pack! show!) ) )

You have a superfluous set of parens around your function body. Compare to the following:

(defn -main [& args] 
  (config! f :content "Hello world.")
  (-> f pack! show!))

Upvotes: 4

Related Questions