Sunday, May 20, 2007

Scala




What is scala? Well folks, it is a statically typed programming language. It is based on OOP paradigm, and it's pretty pure on that - everything is an object, it smells like java here. But java is cheating, and as for now, I can't see any cheating from scala's syntax point of view. What's on my mind is that java distinguishes primitive types from reference types, and also functions are not objects. Right, what is so great about scala? Well, you can run scala from your java and .NET code! Can you see that, this is true write once run everywhere, you don't have to choose between .NET and Java anymore. Scala was pretty much at the end of the list of stuff that I was going to write about, but recently it was used on JavaOne, so I thought that it might be good to write about it now. Scala was developed by Martin Odersky's group. Before that time Martin was the author of current java compiler. Some people saids that this language is the java of the 21 century, SUN cannot change java syntax to scala cause it has to keep the backwards compatibility with java.

You can import any java library to scala and you can import any scala library to java. That's a big plus. It reminds me some presentation from JavaOne in 2006 when somebody said that SUN is promoting java not as a language but as a framework. It means that java has a huge number of frameworks that one can use, it also have many tools, and apps already written - just look at those application servers. What it really sucks on is the language itself - it miss so many things. Lets' go back to scala.

Scala has some functional language specific features but it is NOT a functional language. I've read in a few places that it is, but it is not. Scala is an imperative language!!! If you are interested of what it exactly has, be sure to read this short summary.

Personally I do not like the syntax of it sometimes. For example functions:


def foo(callback: () => unit) {
...
}


what's that, just look at this thing. Why not call callback a function, or do something like this:

def foo(function bar){
...
}


I know, it's because we want to show what we are returning. It's all because of strong typing. Otherwise strong typing purists would scream that it might be confusing, just like they did with type inference in java. But why not to do a literal for that? Or some AI for the compiler - actually this kind of AI is already implemented, cause compiler can guess a return type for methods:


class Foo(bar: double) {
def buzz() = bar
}


Here, I don't have to specify a return type. For those of you who didn't cache that one I'm a week type languages supporter, I just hate to write more than I really have to. And this syntax is quite alike anonymous function syntax so I won't say any bad word anymore.

Let's rock and roll
First thing to do is to configure your favorite environment. Scala's distribution ships with modes for many general use apps. As for 2.4.0 version it binds to: a2ps, emacs, gedit, intellij, jedit, kate, scite, vim, xcode. They are installed in the ${SCALLA_INSTALL_DIR}/share/scala/misc/scala-tool-support. Notice that it's not very common that a tool supports intellij in a standard distribution and not eclipse. Fear not, eclipse plugin is located here. Here is my configuration for GNU/Emacs.

(defvar utils-dir "/home/sagasu/worek")
(setenv "SCALA_HOME" (concat utils-dir "/scala"))
(setenv "PATH" (concat (getenv "PATH")
":" (getenv "SCALA_HOME") "/bin"))
(require 'scala-mode-auto)


Scala and Maven
As for now there is no scala plugin for maven that is ready to use. Google is sponsoring creation of a plugin. So, it's not in any maven repository but you can build it yourself. Grab it from subversion repository:

svn checkout http://maven-scala-plugin.googlecode.com/svn/trunk/ maven-scala-plugin

and build

cd maven-scala-plugin
mvn install


now we can add following lines to our maven project (pom.xml file) in order to compile any scala program:


<build>
<plugins>
<plugin>
<groupId>scala</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<id>scala-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<uncheckedWarnings>true</uncheckedWarnings>
<deprecationWarnings>true</deprecationWarnings>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>scala</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<id>scala-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

No comments: