Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much interoperability is there between C++ and Scala?

Tags:

c++

interop

scala

Everybody knows about Java and Scala, but how much interoperability is there between C++ and Scala. Can classes from one be be used by the other, for example?

like image 367
Henry Henrinson Avatar asked Aug 12 '11 08:08

Henry Henrinson


People also ask

Is Scala fully interoperable with Java?

Scala source code compiles to Java bytecode, so it can execute on any JVM. It also provides complete interoperability with Java, and hence, we can reference Java from Scala and vice versa with ease.

Is rust similar to Scala?

Rust type system is also extremely powerful and similar to Scala. Rust supports Algebraic data types (ADTs) thanks to its powerful enums and also supports advance pattern matching.

Why is Scala compatible with Java?

Scala is a statically typed language that is based on Java. Thus, anyone who's well-versed with Java's syntax will find it pretty easy to learn Scala.

Can we call Scala by Java and Java by Scala?

Yes, there is the ability to mix both types of code. It is possible to create an SBT project, put Scala code in src/main/scala and java code in src/main/java in the same project and make it work.


3 Answers

It's not that simple at all. Java and Scala work on the same virtual machine and scala has been designed to work well with java.

C++ generates native code, and even each compiler has its own way to generate that native code. That means that some class compiled with Visual Studio won't interact easily with another class compiled with gcc.

If you need to interact with C++, you need to use some glue with JNI http://en.wikipedia.org/wiki/Java_Native_Interface. I believe it should be straightfoward to use it with scala. You can make interaction somewhat more comfortable using Swig http://www.swig.org/

like image 78
Tristram Gräbener Avatar answered Oct 29 '22 06:10

Tristram Gräbener


To make this kind of interoperability even possible, both implementations must compile for the same intermediate language (i.e. Java bytecode, .Net IL, LLVM). Scala.Net and C++/CLI is the closest combination — both produce code for .Net. But even then it's not that simple, as a class in Scala and a class in C++ stand for slightly different things.

Typically, Scala stands for Scala for Java VM, and C++ stands for C++ for native code, so general answer should be “not much more than between any other two randomly picked languages”.

like image 39
hamstergene Avatar answered Oct 29 '22 07:10

hamstergene


I would've checked out JNAerator. It builds bridges from C/C++/Obj-C header files to Java interfaces.

like image 39
thoredge Avatar answered Oct 29 '22 07:10

thoredge