Sunday, October 10, 2010

Installing The Scala Developer Documenation

Sometimes it is useful to have an offline version of the scala api docs installed. To install the docs you can use the sbaz tool:

sbaz install scala-devel-docs

This command installs the following documentation to $SCALA_HOME/doc/scala-devel-docs:

  • the scala api docs
  • the reference for the scala tools like scalac and sbaz
  • some code examples

Tuesday, October 5, 2010

Scala Tutorial Part 0 - Four Ways To Run Your Scala Programs

So you have downloaded the scala distribution from scala-lang.org. $SCALA_HOME points to your scala installation and $SCALA_HOME/bin is in your PATH. Now you want to write your first scala program. This tutorial is going to show you four ways to run your first scala program. 


The interactive scala shell

The interactive scala shell can be started be typing the command  scala on the console. The scala shell allows you to enter scala code that is executed immediately.

The following listing shows how to run the classical "hello world" program:
$ scala
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> println("hello, scala world!");
hello, scala world!

scala> 

To quit the shell hit CTRL-D.


Running scripts

If you want to run the same code multiple times, you can put the code into a script. Simply put the above code into a file called HelloWorld.scala. To execute the script type
scala HelloWorld.scala
If you run scala programs interactively or as scripts you don't need classes. Especially you do not need a main class. This allows you to write scala scripts without lots of boilerplate code. But if you want to compile the code, you need to put your scala code into classes.

Running compiled code with the scala interpreter

To create a compilable version of our hello world application you need to create an object. Objects are the scala way to create static methods. We will explain more about objects in a future tutorial in this series.
Put the following code into a file called HelloWorldCompilable.scala:
object HelloWorld {
    def main (args: Array[String]) : Unit = {
        println("hello, scala world")
    }
}
If you are familiar with java you can understand this code. Unlike java scala allows you to give the file containing a class a different name than the class name.

To compile the code type
scalac HelloWorldCompilable.scala

The scala compiler creates two class files: HelloWorld.class and HelloWorld$.class. To run the compiled code type
scala HelloWorld
Make sure to omit the suffix .class. Typing scala HelloWorld.class will result in an error.


Running compiled code with the java interpreter

The scalac compiler creates java bytecode. Because of this you also can use the java interpreter to execute our HelloWorld class. All you have to do is to put the scala-library.jar into the classpath. This jar is located in the lib directory of your scala installation. To run the program type
java -classpath $SCALA_HOME/lib/scala-library.jar:. HelloWorld

This last example shows you one of the great strengths of scala. The fact that scalac generates java bytecode makes it possible to access every java library in scala code!

Monday, October 4, 2010

Subversion Clients on Mac OS

For Java development always use the eclipse subversive svn plugin.

Today i evaluated other svn clients for mac os. Of course there is the command line client svn. But that is not always comfortable. So let us have a look at different gui frontends for subversion.
 
The Textmate Subversion Bundle 

Textmate is my favorite text-editor for Mac OS. Textmate is a commercial tool which costs nearly 50 euros but it has so many useful features that i think it is worth the money if you are using your mac as a development machine.

The subversion bundle can be used to check out a project from a subversion repository and to work with the files from the project. It offers basic support for the add, remove, commit, diff, info and merge commands. Even the blame command is supported.
The Textmate blame window

The Subversion context menu in Textmate


As you can see the Textmate subversion bundle allows you to work with files in a working copy. But there is no support for importing projects to a repository or to create tags using the copy command. 

Conclusion:
Given this feature set, the subversion bundle is an option if you are developing using Textmate. But you will need another tool for the administration of the repository.

Finder Integration with SCPlugin

The SCPlugin aims to offer finder integration for subversion. The current version is 0.8.2. The SCPlugin project claims to support Snow Lepopard. I followed the installation posted here, but the plugin does not work for me. I can use the svn actions but i do not get icons that show the state of the files in my working copy. Did anyone get this to work? 

Conclusion:
For me SCPlugin seems rather useless.

SvnX

SnvX is a free standalone gui tool for subversion. It offers support for most of the svn client commands. It supports both managing files in your working copy and managing your repository.

The SvnX Gui

Conclusion:
This tool is the opposite of the Textmate subversion bundle: Repository administration works pretty good with this tool, managing a working copy is partially supported but not very comfortable.



Commercial SVN Clients

Besides the tools menitoned above there exists a range of commercial SVN clients. Since i do not want to pay for an snv client i did not evaluate them. Below is a list of commercial svn clients for Mac OS:


Summary

Despite the fact that subversion is being used productively for many years now the support for svn in Mac OS is not very good if you do not want to buy a commercial client app. I will continue using a combination of the command line client, svnx, textmate and eclipse. There is no suitable all in one solution for me. 

Do you have better solution?

Sunday, September 12, 2010

Scala resources

A google search for 'scala language' returns more than two million hits.
In this post i am collecting links to scala related resources and articles i consider useful.

Link Description
Learning Scala List of resources for learning scala on scala-lang.org.
artima scalazine Online Scala magazine that features several articles by scala creator Martin Odersky
Programming Scala Free online version of the book "Programming Scala" with user comments.
Scala series at IBM developerWorks IBM developerworks features a great series with articles about programming in scala.
Code Monkeyism - the blog for developers Blog containing some interesting articles about scala.

Installing Textmate Support For Scala

To install a scala bundle for Textmate you can use the Scala Bazaar System.

The sbaz binary is located in $SCALA_HOME/bin.
> sbaz install scala-tool-support
planning to install: scala-tool-support/2.8.0.final
Installing...

This downloads several scala-tools to $SCALA_HOME/misc. In particular it creates the folder
$SCALA_HOME/misc/scala-tool-support/textmate/Bundles/.

To install the scala-bundle simply unzip Scala.tmbundle.zip:

>unzip Scala.tmbundle.zip

Then doubleclick Scala.tmbundle. Thats all!

Tuesday, August 17, 2010

Basic usage of actors in scala

Summary:
This article gives a short introduction about parallel programming using scala actors.


 
The Scala programming language makes it easy to execute tasks in parallel. The easiest way to start parallel execution is to call the actor method of the Actor companion Object. 


This method takes a code block as parameter and creates a new thread executing the code block.

actor {
  // code to be executed
}

Note for java developers: using scala style the brackets around the method arguments are omitted here. The code shown above is equivalent to

actor({
  // code to be executed
})

The following example shows a small scala script that creates two actors. Each of the actors says hello for five times. After having sayed hello each actor takes a break for a small time.

import scala.actors.Actor._ 
for (actorId <- 1 to 2) {
  actor {
    for (helloIndex <- 1 to 5) {
      printf("Actor %d says hello  %d times.\n", actorId, helloIndex)
      Thread.sleep(30)
    }
  }
}


To run this example simple start the scala interpreter in interactive mode and paste the code into the command line.
If i execute the above script i get the following output:

Actor 1 says hello  1 times.
Actor 1 says hello  2 times.
Actor 2 says hello  2 times.
Actor 2 says hello  3 times.
Actor 1 says hello  3 times.
Actor 2 says hello  4 times.
Actor 1 says hello  4 times.
Actor 2 says hello  5 times.
Actor 1 says hello  5 times.

As you can see the messages from the two actors appear intermixed because the actors are executed in parallel. 


If you want to run the example in eclipse simply create a scala object with a main method and run it as scala application.

import scala.actors.Actor._ 
object SimpleActorDemo {

  def main(args: Array[String]): Unit = {
    for (actorId <- 1 to 2) {
      actor {
        for (helloIndex <- 1 to 5) {
          printf("Actor %d says hello  %d times.\n",
             actorId, helloIndex)
        }
      }
    } 
  }
}


Did you find this article useful? Your comments are greatly appreciated.