Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Does anybody know why was Scala implemented in Java and .NET instead of C or C++? Most languages are implemented with Cor C++ [i.e Erlang, Python, PHP, Ruby, Perl]. What are the advantages for Scala implemented in Java and .NET other than giving access to Java and .NET libraries?

UPDATE

Woudln't Scala gain more benefit if it is implemented in C because it can be tuned better rather than relying on JVM?

share|improve this question
7  
there's no such language as C/C++, which is one reason. Josh mentioned another. – jwenting Apr 20 '11 at 6:52
3  
Also, being able to use existing Java libraries and tightly inter-operate with java code is a HUGE benefit, not some minor thing. – fish Apr 20 '11 at 7:09
1  
@OP: you speak as though its bad to have a language implemented on top of the JVM (or CLR for that matter). The tuning you mention that is possible in C is nowhere near the amount of tuning put into CLR or the JVM. And if the platform improves, your language automatically gets it for free. Given the choice, no one should be implementing their language on top of good'ol C anymore imho. – Chii Apr 20 '11 at 12:44
2  
@Chii, just admit it, Java is still slower than C. – jpartogi Apr 20 '11 at 13:05
6  
@jpartogi, Java cannot be slower or faster than C. They're both languages, not stallions. In some specific conditions, some specific code compiled by Java compiler and executed with a certain JVM is slower than a roughly comparable code, generated by a C compiler. In some other conditions the latter will be slower. – SK-logic Apr 20 '11 at 13:13
show 8 more comments

8 Answers

up vote 15 down vote accepted

The question is confusing, as C and C++ are languages, while JVM is a virtual machine and .Net is a platform. Scala could be implemented in C or C++, and it could generate machine code instead of bytecode for a virtual machine.

Answering the question that was asked:

Scala was not implemented in C or C++ because Scala, the language in which it is actually implemented, is a much better language.

Why is it better? Well, go read about Odersky goals for the Scala language.

Answering the question that may have been intended:

Scala generates primarily JVM bytecode because that provides great portability as well as features such as a reliable and efficient garbage collector, run-time optimizations and just-in-time compilation by the JVM.

Let me repeat that last thing: JVM will compile to machine code hot spots in the code it is running. That's compile just like C and C++ compilers do.

There are other virtual machines available, but Odersky, Scala's creator, was already very familiar with JVM. He intended to have CLR as an alternative, but the effort to get that going hasn't achieved success yet.

Answering the question that could/should have been asked:

Compiling to machine code doesn't provide enough benefits over compiling to JVM bytecode.

It is certainly possible to generate microbenchmarks in C or C++ that beat JVM-equivalents. It is also true that extremely optimized code in C or C++ will beat extremely optimized code in Java or Scala. The difference isn't all that great, however, for long-running program.

Note that Scala isn't a particularly good scripting language precisely because the overhead for short-running programs is too big.

However, in most cases the speed of development and or easy of maintenance and debugging is more important than the speed of execution. In those cases, where people are more concerned in writing very high level code that is easily understand and change, the run-time optimizations provided by the JVM may easily beat compile-time optimizations made by C or C++ compilers, making JVM (and CLR) the target that will actually execute faster.

So, no matter whether the question was about Scala compiler being a machine code executable, or Scala programs being machine code, the potential speed gains do not, necessarily, translate into real speed gains.

And, by the way,

I'll give you a counter-example: Haskell. Haskell generates machine code, and, yet, Haskell programs fare worse on Debian's shootout than Scala's. Given that, can anyone be sure Scala programs would be faster if compiled directly to machine code?

share|improve this answer

One of the big hurdles languages face when being introduced to the world at large is library availability. The traditional response to this has been to provide a C-based FFI (foreign function interface) to permit you access to C-based libraries. This is not ideal for a variety of reasons, chief among them:

  • There's a lot of different ways that libraries want to interact that aren't compatible with many higher-level languages. For example if the library wants a pointer to a struct, how do the languages with no pointers AND no structs cope?
  • There are harsh interactions between memory models of different libraries and languages which are often not resolvable or, if resolvable, are highly error- and bug-prone.
  • The glue code for many FFIs is non-trivial and assumes knowledge that may not, in fact, be universal. (Believe it or not, not all programmers are C gurus, and neither do they want to be nor should they be required to be!)

This gets even worse with C++. C++ isn't even compatible with C++ (at a binary level, I mean) from compiler to compiler on the same platform (!), not to mention with other languages.

Targeting the JVM solves many of these problems while giving you access to the absolutely enormous suite of Java-based libraries. (How enormous? Just scope out The Apache Software Foundation's huge selection for starters.)

  • Java's calling and ownership conventions are more regular than C's.
  • The JVM also provides a single memory model (including garbage collection) for languages and libraries both to interface with. There's no need to keep track of who owns what and which has to clean up where. The runtime does it for you.
  • The glue code for FFI, for most languages built on the JVM, is non-existent (as in it's provided as a framework behind the scenes in the language). There's no need to program in Java, for example, to access Java libraries in Scala, Clojure, JRuby, etc. You access the Java objects the same way you access native "objects" (scare quotes because, for example, Clojure doesn't have actual objects in the OOP sense) and in your native language.

On top of these advantages you also have the added advantages of running anywhere Java runs without recompilation (but with testing!: write once, test everywhere) and having access to Java's rather impressive JIT technology.

The CLR provides similar strengths, but adds what is IMO a weakness: it's pretty much a vendor lock-in environment. (Yes I know about Mono. I still think it's a vendor lock-in environment.)

share|improve this answer
1  
You do realize that C# and the CLR are actually an open standard any one can use. – Erin Apr 20 '11 at 14:02
1  
I think the bit about where I "know about Mono" and then "still think it's a vendor lock-in environment" should give you a bit of a clue there, Erin. – JUST MY correct OPINION Apr 20 '11 at 14:07
1  
@Erin not all of the .NET Framework is though – alternative Apr 21 '11 at 0:12

All the other languages you mention, Erlang, Python, PHP, Ruby, Perl -- these were all created before Java & .NET. If the creators of those languages had the Java or .NET runtime environment available to them at the time, then it's possible they might have leveraged those when building their language.

Of course, I can't speak for the developers of those languages, so I can't say for sure that they would've used .NET and/or Java when building them had they been available, but it seems to me like a good idea. After all, by designing your language to compile to Java/.NET bytecode, you get all of the advantages of the JIT compilers/optimiziers, your language automatically runs on all platforms that Java/.NET runs on, you have access to all of the Java/.NET libraries and so on.

share|improve this answer
2  
The advantages described are some of the reasons that e.g. Python has been reimplemented both for JVM (Jython) and .NET (IronPython). – dancek Apr 20 '11 at 8:29
That makes sense. But I guess if it is implemented in C you can tuned it much better. – jpartogi Apr 20 '11 at 8:32
-1: Assuming that new languages could have been dependant on a specific platform (.Net or JVM) because they would be available don't look like a good argument to me. For example, I dont' see any good reason for Python or Erlang to run on such platform. Hystory don't say it all. – Klaim Apr 20 '11 at 9:58
@Klaim: I'm not saying it would be a no-brainer in all cases, but I can see why it might've been useful for languages like PHP or Python. As @dancek says, IronPython and Jython show that there must be some value. – Dean Harding Apr 20 '11 at 10:38
1  
Sorry I was not clear, what I meant is that it wouldn't have it's had it's "success" (PHP or Python) because worknig over a JVM or .Net imply a lot of things that would have annoyed a lot of developpers, making them more niche language than they are currently. On the technical side, the platform (.Net or JVM) would have been a problem because it does drive the way you build a language over it. Stating with the machine is a way to make exactly the language you want. So with or without JVM available, I see 0 good reason to build over .Net and JVM. Other than rapid implementation. – Klaim Apr 20 '11 at 12:02
show 7 more comments

According to this interview, access to existing Java infrastructure and libraries was the primary reason.

share|improve this answer
+1 for a reference, rather than speculation. – Sean McMillan Sep 28 '11 at 13:14

JVM / CLR

The JVM (and the CLR) provide unique advantages in terms of optimization and code portability.

As far as I know, only the JVM version of Scala is being kept current, the .NET version is not.

share|improve this answer

It looks like you are mixing two unrelated things.

The first one is, which programming language is used by Scala author(s) to implement Scala?

To which the answer is, Scala itself. And it's the only acceptable answer, really, because if you have invented this new language, but don't use it yourself for implementing it - what good is it for?

The second thing is, what is the target platform for running programs written in Scala?

Here the choice becomes more interesting, but for now, the only target that works 100% is JVM. Support for .NET is still work in progress. Also, some people are working to get Scala to compile to javacsript. In theory, nothing prevents someone from adding more 'backends' for compiling to C, C++, LLVM, native code or whatever.

Why JVM was chosen as primary platform? My guess is because

  • everyone wants garbage collection
  • large number of good libraries ready to use
  • large number of programmers bored with Java ready to jump to something new, but stay withing the limits of JVM (no one wants to migrate their existing code to another platform)
share|improve this answer
I don't see why a garbage collector cannot be implemented with C or C++? I don't see that as a good reason. Python have done it. Ruby have done it. Heck even erlang have done it. Who knows that Scala might end up with a better garbage collector if it is written with C or C++? – jpartogi Apr 21 '11 at 5:03
I meant 'real' garbage collection. I don't think that garbage collection that provokes questions like this one is good enough. Heck even JVM is not good enough - otherwise people like AzulSystems would not be able to make a living by helping other people to overcome JVM deficiencies. – artem Apr 21 '11 at 5:21
Also, libraries. It's really difficult to use libraries written for explicit memory management in a language with garbage collection. One indication is peculiar insistence of java people to have everything in 'pure java'. – artem Apr 21 '11 at 5:35

JVM written in C, but javac is in Java.

share|improve this answer
1  
The JVM's written in C? I thought it was written in C++ (based on the fact that HotSpot came from the Animorphic guys' Strongtalk VM, which was/is implemented in C++). – Frank Shearar Apr 20 '11 at 11:00
OK, I should have said C/C++ (that mythical language). I've no idea really, but I thought it was C. In any case my real point was that javac is in Java (like scalac is in Scala). It compiles to the JVM (or .NET) largely to take advantage of those ecosystems. – Bruce Stephens Apr 20 '11 at 11:22

It's not clear that having ability for better tuning would be a good tradeoff. JVMs can do optimization at runtime, and that's commonly at least good enough, if not superior to what typically happens with static compilation. (Obviously in principle for a specific application and workload it ought to be possible to beat JIT with static optimizations, but practically you don't often have the precise workload or even the whole application.)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.