Since Clojure runs on the JVM, you can access any known class that is in your class path from Clojure. Here is an example of a Scala. To simplify class customization and dependency management, use Leiningen to create a project.
lein new clojure-scala
In the project folder, change project.clj and add the dependency for the Scala language libraries and the scala -src folder to the class path:
(defproject clj-scala "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[org.clojure/clojure "1.3.0"] [org.scala-lang/scala-library "2.7.1"]] :compile-path "scala-src" )
Create the scala -src directory, and in this folder create the following Scala class:
class HelloWorld { def sayHelloToClojure(msg: String) = "Here the echo message from Scala: " concat msg }
Compile the class with scalac. Now run lein deps to load the dependencies. Launch Clojure REPL by replacing lein. You can import the Scala class, instantiate it, and call the sayHelloToClojure method.
user=> (import HelloWorld) HelloWorld user=> (.sayHelloToClojure (HelloWorld.) "Hi there") "Here the echo message from Scala: Hi there"
This is just compatible with how you can use Scala classes and code from Java. This may seem complicated, quote from Frequently Asked Questions - Java Interoperability :
Using the Scala class from Java can be difficult, especially if your Scala class uses advanced features such as generics, polymorphic methods, or abstract types. Since Java does not have such language features, you should know something about the Scala class coding scheme.
raju-bitter
source share