Scala on Microsoft .NET

This is a guide on using Scala version 1.4 on the Microsoft .NET platform.

Requirements

Apart from the Scala software distribution you need to install the following software:

Configuration

Then you need to modify your working environment as follows:

  • Copy the file mscorlib.dll from the Microsoft .NET directory to the share\scala\lib directory of the Scala installation directory. The Scala compiler will need it (see usage of the option -r in the example below) to access the metadata of the Microsoft .NET runtime library.
    C:\temp>copy %CLR_HOME%\mscorlib.dll %SCALA_HOME%\share\scala\lib
    
    NB. For convenience we assume here that the environment variable CLR_HOME is defined as follows:
    Name: CLR_HOME
    Value: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
  • You may now set up your environment in two different ways in order to run the .NET executables generated by the Scala compiler:
    • If you have administrator privileges, than register the assembly file %SCALA_HOME%\lib\scala.dll into the Global Assembly Cache.

       

      Hint: You will find the .NET configuration tool starting from the Start menu of the Windows taskbar and following the steps below:
      • Control Panel → Administrative Tools → Microsoft .NET Framework Configuration
      • In the "Assembly Cache" settings, we then choose the task "Add an Assembly to the Assembly Cache" and select the scala.dll file from the Scala installation directory.
         
    • Otherwise you will need to copy the file %SCALA_HOME%\share\scala\lib\scala.dll in the same directory as the executable.

Test Example

As our first example, we use the standard Hello World program.

import System.Console

object test extends Application {
  Console.WriteLine("Hello world!")
}

To compile the example, we use the Scala compiler with the following two options:

C:\temp>scalac -target:msil -r "%SCALA_HOME%"\share\scala\lib test.scala

and we give the generated assembly file test.il as input to the IL Assembler:

C:\temp>ilasm /output=test.exe /quiet test.il
NB. For using the IL Assembler you may need to modify the environment variable PATH as follows:
Name: PATH
Value: ...;%CLR_HOME%\;...

Finally, to run the example, we enter the following command:

C:\temp>test

and get the following output:

Hello world!

Additional Information

Once you've compiled your first Scala program for .NET you may want to dig into more details on the quirks of Scala for .NET, or how .NET specific features map to Scala.

Copyright © 2010 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland