10

It's just a stupid question that I had this morning : Can we use Scala classes from clojure ?

Because if the answer is yes, I'll definetly learn Clojure ^^

1
  • 1
    Why the scala-2.8 tag? Is there anything specific to that version? Commented Aug 24, 2010 at 11:21

3 Answers 3

17

Scala classes are Java classes and you can use Java classes from Clojure quite conveniently.

Have fun learning Clojure. :-)

Sign up to request clarification or add additional context in comments.

12 Comments

And Cloujre classes (created using gen-class and deftype/defrecord) can also be used from Scala. It's all bytecode underneath.
Unfortunately, yes. Like blank? becomes blank_QMARK. That is because Java (or JVM?) does not support such characters in the identifiers.
With gen-class or, say, definterface + deftype (or defrecord) you can call your methods whatever you like. Note that Clojure itself only uses classes as an implementation detail (at a low level the user doesn't necessarily need to know about) and (at a high level) in interop scenarios. Pure Clojure code is built around totally different concepts than Java code; if you want to mix Clojure and Java / Scala class-based code, you'll likely get the best mileage by implementing solid chunks of functionality in Clojure and then exposing wrapper classes (which is fairly straightforward to do).
@abhin4v: Sure, but if you actually wanted to call a Clojure function from Java, you'd do something like clojure.lang.RT.var("clojure.core", "even?").invoke(2), avoiding the nasty munging. (That's equivalent to (#'clojure.core/even? 2); alternatively, you could extract the function from the Var with .get and .invoke that.)
@oxbow_lakes: For example, if Scala had a great implementation of 7zip compression, I might call out to that from Clojure to avoid writing my own version. Or if it had a great parser for some kind of input I was interested in, it could be easier to write a converter to turn the Scala data structures returned by that parser into Clojure data structures than it would be to reimplement the whole parser (I've done something similar with ANTLR). In fact, this is what I understood the question to mean (note that it's about "using Scala classes from Clojure" and not "writing Scala code in Clojure").
|
10

Scala classes, yes. Scala singleton objects, with the same year signature as from Java. Scala traits, no. Scala implicits, hah, you jest!

So the experience may range from "ok", if it's just your classes, to very disagreeable, in the case of Scala collections, for instance.

1 Comment

I don't know about implicits, but traits are interfaces, possibly coupled with helper classes. Daniel Spiewak has a nice write-up on Scala/Java interop including a section on using traits from Java; judging from that, it gets tedious for traits which are not "pure abstract", but in Clojure much of that tedium -- perhaps nearly all -- could be abstracted away with macros.
2

Worth saying that Scala classes which take function parameters, such as:

Option.map(f : A => B)

Are unlikely to be very useable from Clojure, because the Clojure compiler will not turn Clojure lambdas into instances of scala.FunctionN. I'm with Daniel - I think interop will be poor.

1 Comment

I'm pretty sure you could use macros to solve the usability issue - basically get the macro to wrap the Clojure lambda into a scala.functionN?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.