By Nicolas Mouso The aim of this post is to present the Scala programming language in the
By Nicolas Mouso
The aim of this post is to present the Scala programming language in the frame of a typical Java development environment, using Maven and Eclipse to compile sources making both languages (Java and Scala) coexist in the same solution.
Scala is a multiparadigm programming language strongly typed that combines the features of object oriented programming and functional programming.
It has many interesting characteristics, such as traits, type inference, pattern matching or implicit functions, which make it a complete and powerful language. You can find more detailed information on the subject at Scala´s site: http://www.scala-lang.org/.
One remarkable feature is that it functions on the Java Virtual Machine, making it possible to reuse existing Java code from Scala.
Scala´s plug-in can be installed directly from Eclipse following these steps: “Help → Software Updates … → Available Software”, and adding the corresponding address to the version we want to install (consult the download section at the official site: http://www.scala-ide.org/).
Let´s start with a simple implementation of the famous “Hello World” to check that the installation was successful. Inside the Scala perspective, follow the next steps: File → New → Scala Proyect”, to create a Scala Project and name it “HelloWorld”. Then create a Scala object with the same name in the following way:
In Scala, classes can´t have static attributes like in Java. However, it gives the chance to create singletons objects using “object” in a simple way, as it can be seen in the case “Hello World”.
Integrating Scala with Maven and Eclipse
Scala can be integrated with other Java technologies in such a way to take advantage of the language, but using the java community frameworks such as Maven, Hibernate, Spring, Wicket, etc.
The first thing to do is install Maven, which process is analog to the one for the plug-in. For more information check the official web site: http://www.assembla.com/spaces/scala-ide/wiki/With_M2Eclipse.
If you have an instance of m2clipse already installed in Eclipse, erase it before continuing. Two maven instances would be installed, which can cause some trouble.
Setting up the project using Maven from Eclipse:
1. File → New → Project
2. Maven → Maven Project
3. Enter “scala” in the filter and select:
Group Id: org.scala-tools.archetypes
Artifact Id: scala-archetype-simple
5. Complete information with:
By default some tests are generated. To check there were no problems in the creation, make a ‘Run configuration’ for the project with the goals: ‘clean install’. If you are using Maven for console, use instead “mvn clean install”.
In case Eclipse doesn´t recognise the project as a Scala one (for instance, it doesn´t allow navigating through classes using F3), follow these steps: “right click on the project→ Maven → Update Maven Configuration…”.
This is how Java and Scala can co-exist, compiling with Maven and in Eclipse.
Scala has the advantage of compiling a byte code, which allows running any Scale program over Java virtual machine, making it easy to integrate with Java technologies. Therefore you can take advantage of:
1. the excellent benefits that Java´s Virtual Machine provides in terms of performance and security.
2. the countless libraries and frameworks that exist for Java, which can be used without having to start from scratch.
3. Scala as a language, which provides an alternative to Java.
- Scala Programming Language, Dean Wampler & Alex Payne, O’Reilly
- Roundup: Scala for Java Refugees, http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees
- Scala IDE for Eclipse, http://www.scala-ide.org/