Question

I'm looking for a guide for polyglot programming in this two languages.

I know that interop between them is possible, since they are both running on the same Java VM, and both got compiled to the same bytecode. But there is some cumbersomes:

  • Can I use to compile Clojure code?
  • Can I use to compile Scala code?

( Yeah, yeah, I know I can just plug in jar from one language to project in other lang.)

So the question is How to setup polyglot development in Scala and Clojure?

Was it helpful?

Solution

I've not used it, but Leiningen does have scalac support.

OTHER TIPS

The short anser: use Maven3

maven3 with the zi plugin for clojure and the maven-scala-plugin will allow you to have a nicely integrated polyglot project where you can use any language you want as long as its Java ;)

One of the big changes in Maven3 was a push toward polyglot JVM programming including the ability to write POMs in many languages. You loose the polish of leiningen when taking this route, though you gain plenty in return.

Personally, I'd stay away from using Maven directly despite being the accepted answer. Here is the simple Leiningen project definition which:

  • generates Java sources from ANTLR4 grammar (as a bonus);
  • compiles Java sources;
  • compiles Scala sources (I prefer to use Scala on top of generated ANTLR4 Java code since it's much more concise and pleasant to work with);
  • continues with Clojure stuff.
(defproject polyglot "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"}
  :plugins [[lein-antlr4 "0.1.0-SNAPSHOT"]
            [lein-scalac "0.1.0"]]
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [org.antlr/antlr4 "4.0"]
                 [org.scala-lang/scala-library "2.10.1"]]
  :warn-on-reflection true
  :antlr4-source-paths ["antlr4"]
  :antlr4-options {:package "parser" :listener true}
  :antlr4-compile-path "target/antlr4/parser"
  :java-source-paths ["target/antlr4/parser" "src/java"]
  :scala-source-path "src/scala"
  :prep-tasks ["antlr4" "javac" "scalac" "compile"])

To use ANTLR4 plugin download and 'lein install' the lein-antlr4 plugin. If you don't need it, just remove relevant lines from the project definition.

To use Scala plugin I needed to download it and change

[org.scala-lang/scala-compiler "2.9.1"] 

to

[org.scala-lang/scala-compiler "2.10.1"]

in plugin's project.clj, then 'lein install' it locally. With older dependency version I was getting

java.lang.Error: typeConstructor inapplicable for <none>

from Scala compiler.

As a maven alternative: gradle is able to compile both languages.

Scala's Dynamic trait was created to enable easier integration with dynamically typed languages, but you'll have to write your own forwarder. Also, you'll have to rewrite it with Scala 2.10, since its been changed a bit. Perhaps, in fact, you should use Scala 2.10 (milestone 2 is available). Scala 2.10 CAT support may also help you with integration on the Scala side.

You can use SBT to compile Clojure, since SBT is very flexibly, but, more pragmatically, there is no Clojure-compilation support readily available. You can look at this plugin as an example of how to add compilation of other languages.

I find it curious that so little integration between Scala and Clojure seems to exist. They are often compared, and you'll often see people saying it choose one over the other.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top