Scala and Clojure both in one project
Asked Answered
G

5

29

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?

Gehlbach answered 13/3, 2012 at 0:39 Comment(3)
For reducing boilerplate, I don't know of any existing libraries. But between Clojure macros and Scala implicit conversions, you can probably go a long way.Shannashannah
Don't ask more than one question on one question. It's unfair to people who can answer of part but not the other, and the accepted answer may not be the best answer for one aspect.Ideomotor
@DanielC.Sobral Generally, I'm trying to avoid that situation. For that reason I've wrote last line. The questions in body describes what I want to see in that guide (I've expected link to some post or whatever).Gehlbach
T
9

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

Talton answered 13/3, 2012 at 12:7 Comment(0)
S
17

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.

Soldo answered 13/3, 2012 at 0:49 Comment(1)
+1 for this: Maven is great for polyglot coding on the JVM. I have slightly less experience with Scala+Maven, but it works great for Java and Clojure in the same project.Sable
T
9

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

Talton answered 13/3, 2012 at 12:7 Comment(0)
T
6

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.

Torpor answered 22/5, 2013 at 9:49 Comment(0)
K
3

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

Kidder answered 13/3, 2012 at 6:45 Comment(0)
I
3

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.

Ideomotor answered 13/3, 2012 at 16:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.