« Somebody set us up the brain | Main | Fear and loathing in Scala 2.7.2 for .NET »

January 01, 2009

Getting started with Scala on .NET

The Scala language has been getting a lot of buzz recently, but I hadn't realised until recently that Scala can compile to the .NET CLR as well as to the Java virtual machine.

Unfortunately, CLR support lags behind JVM support and the CLR support documentation on the Scala Web site leaves out a few steps.  So here is a summary of what you'll need to do to compile a Scala program to target .NET.  These notes are written with reference to Scala 2.7.2; see also the remarks at the end.

1. Ensure your machine's copy of Java is up to date.

2. Download Scala and unzip it to your folder of choice.

3. Install the .NET support.  To do this, open a command prompt in the Scala bin directory and type sbaz install scala-msil.  (sbaz, Scala Bazaars, is a Scala tool for downloading, installing and managing packages, similar to Ruby gems.)  This adds scala-net (the Scala interpreter) and scalac-net (the Scala compiler) to the bin directory, and adds a couple of required .NET assemblies to the lib directory.

4. Write your program.  I'm going to assume it's in a code directory under the Scala directory, in a file called test.scala.

5. Compile your program to MSIL.  To do this, open a command prompt in the Scala directory and run bin\scalac-net code\test.scala.  The output from this will be one or more IL files.  (Even if you have only a single source file you may see multiple IL files.  The compiler seems to emit one IL file per class, plus one for the pot.)  By the way, your firewall may pitch a hissy fit during this stage: this is because scalac-net wants to use the "fast Scala compiler," which runs as a separate process and stays resident so as to avoid having to spin up a JVM every time you compile, and this requires a loopback network connection for the inter-process communication.

6. Compile the MSIL assembly language into a .NET assembly using ilasm.  (Don't look slack-jawed at me.  You heard.)  To do this, open a Windows SDK command prompt, go to the directory containing the MSIL files, and run ilasm code\test.msil code\MyClass.msil (or whatever files Scala has generated).  (This will generate an EXE; provide the /DLL switch to generate a library.)

7. Copy predef.dll and scalaruntime.dll from the Scala lib directory to the directory where you compiled your .NET assembly (in this example, the code directory: copy lib\*.dll code).

8. Run your EXE and experience the Hello Worldy goodness.

It's not actually meant to be quite as horrible as this.  (Update: it is meant to be as horrible as this.)  Steve Gilham reports that, for him, running scalac-net alone sufficed to generate the EXE without all the mucking around with ilasm.  This also works for me using Scala 2.7.1 but not with 2.7.2 or 2.7.3 RC1.  2.7.1 is available from the downloads page via the Previous Versions link.

Note that if you are using 2.7.1 or a fixed future version, scalac-net needs ilasm to be on the path in order to create the EXE directly.  Depending on your setup the Visual Studio command prompt may do the job, or you may need to use the SDK command shell or manually add the .NET Framework directory to the path.  Also note you will still need to copy the Scala DLLs to the target directory: unlike Visual Studio, scalac-net/ilasm does not do this for you.

Okay, so why would you go through all this grief when Visual Studio and C# are sitting there on the nice graphical Start menu promising to love you long time?  Stay tuned.

January 1, 2009 in Software | Permalink


TrackBack URL for this entry:

Listed below are links to weblogs that reference Getting started with Scala on .NET:


Do you know how much of Scala standard library will run on clr?

Posted by: jau at Jan 2, 2009 4:11:39 AM

I've not started digging into the standard library yet, but it looks like it's missing some substantial chunks. Some of these are Java-specific bits, such as the Java collections interop stuff or the various weak reference types, which .NET developers probably won't care about; others appear to be actual major gaps, such as the lack of the actors and parser libraries.

Since the standard library is mostly written in Scala, it may be possible to weed out the Java dependencies in these libraries and get them to compile on the CLR. I might have a look at this at some point.

Posted by: Ivan at Jan 2, 2009 4:17:58 PM

Thank you!
ilasm is not on my path, so I had to call
C:\Windows\Microsoft.NET\Framework\v2.0.50727\ilasm.exe code\test.msil

Posted by: chaecker at Jul 25, 2009 11:19:42 AM

You might need to try opening a SDK command prompt (in the Windows SDK or Microsoft SDKs program group, may be called "CMD Shell"), or a Visual Studio command prompt (in the Visual Studio program group under Visual Studio Tools). Which one(s), if any, include ilasm on the path may depend on which versions of Visual Studio and the .NET Framework SDK you have installed.

Alternatively, of course, you can always add the ilasm directory to your path, or create a batch file to call it. Anyway, if you're planning to play around with Scala on .NET, it's definitely worth doing *something* to save yourself typing the full path to ilasm every time!

Posted by: Ivan at Jul 25, 2009 1:02:04 PM

sbaz is not part of the standard scala distribution. Where do you get it?

Posted by: Andrew Rondeau at Sep 26, 2012 11:46:28 AM

Hi Andrew,

Thankfully, the Scala guys have done a major rework of Scala for .NET since this post was written, and you should no longer need to go through all these shenanigans. Check out https://github.com/magarciaEPFL/scaladotnet or https://github.com/magarciaEPFL/scaladotnet/downloads for the new Scala .NET compiler, and http://lampwww.epfl.ch/~magarcia/ScalaNET/ for more info.

Posted by: Ivan at Sep 26, 2012 12:01:30 PM