Tuesday, October 26, 2010

Is Apple Going To Discontinue Java Support For Mac OS?

Yesterday Apple released Java for Mac OS X 10.6 Update 3. The release notes for this update state:

"As of the release of Java for Mac OS X 10.6 Update 3, the version of Java that is ported by Apple, and that ships with Mac OS X, is deprecated.

This means that the Apple-produced runtime will not be maintained at the same level, and may be removed from future versions of Mac OS X."

What does this mean for the future of Java on Mac OS? At the moment there is no official alternative to the Apple-produced JVM on Mac OS.


The link to the release notes document was found on osnews.
The release notes document can be found here.

Update:
There is an online petition of people who would like to see Apple to contribute the source code of its JDK to the OpenJDK project. The petition can be found here.

Update #2
Apple and Oracle announced the OpenJDK-Project for Mac OS. Apple will donate most of it's code to the OpenJDK-Project. Read more about this here.

Tuesday, October 19, 2010

Scala Tutorial Part 2 - Defining And Calling Functions

Scala is a hybrid language that combines features of object oriented languages with features of functional languages. In this third part of my scala tutorial series i am going to show how to define and call functions in scala. All code snippets shown here can be run using the interactive scala shell or the scala interpreter as shown in the first part of this series.

Functions Without Parameters

The following snippet defines a function that takes no parameters and does not return a value:
scala> def sayHello() { 
     | println("hello")
     | }
sayHello: ()Unit
The scala shell determines that sayHello is a parameterless function that has the return type Unit. The type Unit indicates, that a function does not return a value. For now you can think of Unit as an equivalent of Java's void.
A function definition starts with the keyword def. This is followed by the function name and empty parentheses. Function arguments would go between the parentheses. This is followed by the function body which is surrounded by square brackets.

Functions that return a value have an equals sign between the parameter list and the method body:
def giveUniversalAnswer() = {
     | 42
     | }
giveUniversalAnswer: ()Int     
A function always returns the value of the last statement in the function body. You can mark this by using the return-statement, but this is not usual in scala. As you can see the type inference system determined that this function returns a value of type int. Sometimes the type inference algorithm is unable to infer the return type of a method (e.g. the scala compiler cannot determine the return type of recursive methods). In this case you have to declare the return type explicitly. And of course in some cases it makes sense to declare the return type of a function for the sake of readability.
The return type has to be declared in front of the equals sign:
def giveUniversalAnswer(): Int = {
     | 42
     | }
giveUniversalAnswer: ()Int     
If you omit the equals sign after the parameter list, the function does not return a value. This is even true for functions that have a return-statement.


Command Query Separation

It is good practice to differentiate between functions that have side effects and functions that are free of side effects. A side effect is an operation that changes the state of a program, for instance by changing a global variable or a type field. The practice of differentiating between these function types was introduced by Bertrand Meyer in his book "Object Oriented Software Construction". He introduces the term command query separation for this differentiation.
The basic idea of command query separation is do differentiate between these two function types:
  • queries: functions that compute values but are free of side effects
  • commands: functions that change the state of the system but do not return values.

// Side note:
// A more in depth description of command query separation can be found
// in Martin Fowlers Bliki.

In scala you can use functions without return type to implement commands and functions with return type to implement queries. This way you have a syntax for declaring queries and a syntax for declaring commands.


Functions With Parameters

The following snippet defines a function that takes two arguments:
scala> def registerPerson(name: String, password: String) {                    
     | printf("User '%s' added to database with password '%s'", name, password)
     | }                                                                       
registerPerson: (name: String,password: String)Unit
The parameters of a function go between the parentheses after the function name. Unlike the return type of a function, the type of function parameters has to be declared. The function parameters are constants that cannot be modified inside the function body. The following code will not compile:
scala> def registerPerson(name: String, password: String) {                    
     | name = "not allowed"
     | }
:6: error: reassignment to val
       name = "not allowed"

Calling Functions
Functions are called by the function name followed by the parameter list. If the parameter list is empty you are allowed to omit the empty parentheses. The following code shows two legal ways to call the function sayHello defined above:
scala> sayHello()
hello

scala> sayHello 
hello


// Side note:
// As we will see later, the parentheses can also be omitted
// when calling type members that have only one parameter.

When calling a function that takes parameters, the parameters are passed to the function in parentheses. To call the function registerPerson type
scala> registerPerson("Helmut", "secret")                  
User 'Helmut' added to database with password 'secret'
To make this code easier to read, scala 2.8 introduced the notion of named parameters. These allow you to specify the name of the parameter in the function call. This makes the function call above easier to read and less error prone. Since the name of the parameters is specified you are even allowed to change the order of the parameters passed. The following code demonstrates this:
scala> registerPerson(password="secret", name="Helmut")
User 'Helmut' added to database with password 'secret'


Uniform Access

When declaring functions without parameters, the parentheses indicating the empty parameter list can be omitted. This makes it possible to make the access to a computed value look like the access to a property. When the parentheses for the empty arguments are omitted in the function definition you also have to omit them when calling the function:
scala> def universalAnswer = {     
     | 40 + 2
     | }
universalAnswer: Int

scala> var x = universalAnswer * 2
x: Int = 84
This example does not make a lot of sense. But as we will see later, this feature allows you to define type members that perform a computation but look like fields for clients.


Summary

In this post I gave a short introduction to defining functions in scala. Below is a summary of rules for the use of functions in scala:

  • omit the equals sign in front of the function body if the function does not return a value
  • use an equals sign for functions that return a value, otherwise the return-value is being ignored
  • use command query separation
  • function arguments are unmodifiable constants
  • you can omit the parentheses when calling functions with zero arguments
  • if the parentheses for an empty parameter list is omitted in the function definition, you have to omit it when accessing the function

I hope this article gave you an insight in using functions in scala. Comments, suggestions for improvement or questions are greatly appreciated.

Scala Tutorial Part 1 - Three Reasons To Learn Scala

In this second part of the scala tutorial series I am going to show you three features that make scala an extremely powerful language.
These features are presented using simple one-liners. The code shown in this post can be run using the scala shell. Running scala code snippets using the scala shell is described in the first part of this scala tutorial series.

To get started fire up a scala console:

$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.

Static Typing

In the first step we simply define a variable:
scala> var i = -3
i: Int = -3 
Even in this simple statement you can see one reason why scala is such a powerful language: it has type inference and static typing.
You defined a variable named i with the value -3. The scala console shows you the result of this expression: a variable i with value -3 of type Int was created. The type inference system automatically determined that -3 is an integer. The scala compiler uses this information to ensure that only int values are assigned to the variable i. Let's try this out:
scala> i = "The static type system will not allow this" 
:6: error: type mismatch;
 found   : java.lang.String("The static type system will not allow this")
 required: Int
       i = "The static type system will not allow this"
In the above statement i tried to assign a String to our Int variable. As you can see, this results in an error.

Sometimes the code can be more readable if you include the type of a variable in the variable definition. Of course you can do this, but you don't have to. You could also have written:
scala> var i: Int = -3
i: Int = -3 
As you can see, the type of a variable can be declared after the name of the variable. The type declaration consists of a colon and the type name.


// A little side note:
// the book "Object-Oriented Type Systems" by Jens Palsberg
// and Michael Schwartzbach provides excellent background
// information on the topic type inference.


Concise Syntax

One more thing you can see in the above example is the conciseness of scala code. The scala syntax allows you to reduce the amount of boilerplate code you have to write.

You already learned that you can omit the type declaration. But there is one more thing to see in the above statements: You do not have to terminate statements by a semicolon. At least this is true for most cases - in some cases the compiler cannot infer by itself where a statement ends. In this case you have to help the compiler. But in most cases you are allowed to terminate statements by a semicolon.

As you learn more features of the scala language you will learn to appreciate the conciseness of scala. Here is a little teaser:

To define a Map with some values in scala you can use the following code:
scala> var capitals = Map("Italy" -> "Rome", "Germany" -> "Berlin")
To achieve the same in java you have to write the following code:
Map capitals = new HashMap();
capitals.put("Italy", "Rome");
capitals.put("Germany", "Berlin");
In Java you have to define the type of this map twice! Even if you use third party libraries like google collections from the guava project the java code will still be more verbose than the equivalent scala code.


Scala Is Purely Object Oriented

The third and last power-feature of scala i want to show you in this post can be seen in the following code snippet:
scala> 15.toHexString
res0: String = f
What is happing here? You invoke the method toHexString on the number 15. This is possible because everything in scala is an object. Scala is completely object oriented. It does not have simple types like int. Because of this, the literal 15 defines an object of type Int. Of course you can invoke methods on this object.

Conclusion

This post presented three extremely powerful features of the scala languages:
  • static typing and type inference
  • concise code
  • pure object-orientation

Hopefully I got you interested in scala. Stay tuned for the next article in this series!

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?