Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

What is Scala?

Scala is a modern, multi-paradigm JVM language that is a hybrid of Object-Oriented and Functional styles.  It is useful as complete general purpose replacement for Java.  But its most often compared to Groovy, Clojure and Erlang. Its functional language foundations and built-in Actors library makes make it especially attractive for concurrent computing. (Scala is an abbreviation for "scalable" hinting at its design goals).

In this day and age of multi-core processors , concurrent computing can 't not be ignored. Many of the design features of Scala 's features have been designed chosen with concurrency in mind, primarily a preference for immutability and the use of other functional language paradigms.
Some of these some of which may not be unfamiliar to Objective-C or WebObjects developers. Here's a quick summary:

Collections

 

Objective-C

Java

Scala

Separation of Mutable & Immuable Datatypes

Immutability

Partial - via collections e.g: NSArray/NSMutableArray

No

Yes

Closures

Yes - via Blocks (Extension)

No

Yes - via Anonymous Functions

Static variables

Yes

Yes

No

Static methods or functions

Yes

Yes

No

Concurrency

Yes - via Grand Central Dispatch (Extension)

Yes - via Threads

Yes - via Actors

 

Weakly Typed

Strongly Typed

Strongly Typed

...

 

Objective-C

Java

Scala

Parametered methods

Yes e.g: addObject: to:

No

Yes e.g: add(object= ,to=)

Class composition

Yes - via Categories

Yes - via Interfaces

Yes - via Traits

Why Use Scala?

Scala can help you to write thread-safe code.
It has concurrency that is built-in to the standard library, primarily via Actors.

So for For WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for concurrent applicationscomputing. (In other words, Scala Actors can be used for problems that would have normally required threads).

Can WebObjects be Programmed In Scala?

Yes. It is very simple.
By virtue of being a JVM-language, Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward

Furthermore, being a multi-paradigm language grants Scala easy WebObjects-interoperability.

Caveats

Legacy tool support is often cited as a weak point. The Eclipse Scala plugin has been found to be slow at times and sometimes buggy.

WebObjects In Scala

The following highlights some of the differences between Java and Scala in WebObjects:

EOs in Scala

Thread-Safe Shared Vars

Wiki MarkupScala doesn't have static variables or methods. Instead Scala employs the [Singleton Pattern|http://en.wikipedia.org/wiki/Singleton_pattern] which is built into the language and is *thread-safe*: a class can have a _Companion Object_ that will allow you to achieve something equivalent to static variables - but better. \[Is this true?  I don't think Scala "object" instances (with the object keyword) are guaranteed to be thread-safe; they are just singletons\]So you

You don't have to worry about synchronizing access to shared mutable fields in a concurrent application.
(This is not however true when for example you have a val declared as a NSMutableArray. You will still have to synchronize when adding to or removing from this mutable field).

The following is an example of the use of a Companion Object for Talent in Scala instead of Talent static fields in Java.

...

Code Block
java
java
public class _Talent extends EOGenericRecord {
	public static final String ENTITY_NAME	= "Talent";
}

Scala:

Code Block
object Talent extends EOGenericRecord {
	val ENTITY_NAME = "Talent"
}

This value will be accessed exactly the same way in both languages:

Code Block
Talent.ENTITY_NAME

Compacted imports

Two lines in Java are compacted into one in Scala.

In Java:

Code Block
java
java
import com.webobjects.eocontrol.EOGenericRecord;
import com.webobjects.eocontrol.EORelationshipManipulation;

...

Code Block
java
java
public class MenuHeader extends WOComponent {

    public MenuHeader(WOContext aContext) {
        super(aContext);
    }
}

In Scala:

Code Block
class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) {

Simplified Exception Handling

...

Scala Annotations vs. Generated Accessors

An Here's an example of accessing variables in WebObjects with the following languages:

 

Objective-C

Java

Scala

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fc245d22-e8ed-49bc-bc57-2008421d55af"><ac:plain-text-body><![CDATA[

getter

[WO:object name]

object.name()

object.name

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="93790da0-1ffe-40d9-89c9-80e82f29aebd"><ac:plain-text-body><![CDATA[

setter

[object setName:aName]

object.setName(aName)

object.name = aName

]]></ac:plain-text-body></ac:structured-macro>

Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention.
In scala there is an additional convenience we may use to produce "get" and "set" methods in addition to the default Scala accessors - Scala Annotations.

...

To use the Scala Collections API with an NSArray or NSDictionary you simply need to add an import:

Code Block
java
java

import scala.collection.JavaConversions._

Then After that, you can may access the typical Scala collection methods directly on NSArray.  This employs a feature of Scala known as implicit conversions to automagically cast a NSArray (a Java Iterable) into a Scala Iterable while leaving the actual object unchanged.  Alternatively, you could generate an actual new scala.List instance by calling myNSArray.toList.

How to Add Scala to a WO Project (in Eclipse)

Include Page
WOL:Adding Scala Support to a WOLips Project
WOL:Adding Scala Support to a WOLips Project
Note

This is for Eclipse/WOLips IDE

WO Scala Example

The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app:
All the EO logic and WO components are in Scala.
Only the Application class remains Java.

...

Attachments
patterns.*zip

Setup

  1. Install the Scala eclipse IDE
  2. Right-click on Application.java and run as a WOApplication (as usual).
Note

Application can be made into a Scala class as well, but then you will have to create a launcher in Eclipse manually.

EO Templates

When you create your .eogen file, be sure to make the following changes in the EOGenerator Editor:

...