17

I am familiar that scala classes / objects can be called from clojure, as scala compiles to bytecode, and clojure is comfortable with it.

However is it as painless calling clojure functions, and importing namespaces from scala ?

I would like to mix the excellent lift framework and clojure, basically call clojure code from lift.

murtaza52
  • 46,887
  • 28
  • 84
  • 120
  • I know that people have written bridges for JRuby-Clojure and JRuby-Scala, I'd be very surprised if someone hadn't also done Clojure-Scala. – Jörg W Mittag Aug 13 '12 at 13:04

2 Answers2

14

Semantics for imports in Scala are basically the same as Java. You should be able to get the info you need by reading up on how to invoke Clojure code from Java, then apply the same principles in Scala.

If you want to compile your Clojure code and include it as a JAR in your classpath then this post should be relevant:

Calling Clojure from Java

If you'd rather dynamically compile/interpret the .clj files then you should read this:

Clojure Programming: Invoking Clojure from Java

The first option seems a lot nicer to me.

Community
  • 1
  • 1
DaoWen
  • 32,589
  • 6
  • 74
  • 101
  • 2
    +1, though I think the second option is really worth considering. It's the more "dynamic" choice, which is useful if you are using Clojure as a sort of embedded scripting language. And it avoids the need to pre-compile Clojure classes. – mikera Aug 13 '12 at 06:57
  • There's a small problem with using `var` in scala - you need to put backticks around it. I needed the interop a while ago and made this wrapper: https://gist.github.com/HairyFotr/4995607 – HairyFotr Feb 20 '13 at 13:46
0

FWIW, I had a similar experience recently. It is not always straight forward to consume scala libraries in a clojure codebase. If the library authors have kept the none scala consumers in mind while designing the api, the integration can be trivial. If not you may have to learn the details of what java interface is produced by the scala library you are trying to consume.

I recently wrote a documentation about this exact topic (https://github.com/grandbora/clojure-scala-cantrips#clojure-scala-cantrips) and there are some clojure libraries out there that provides sugar syntax for consuming scala libraries.

grandbora
  • 73
  • 8