WebObjects with Scala

Version 196.1 by Ravi Mendis on 2009/09/16 19:03
Warning
For security reasons, the document is displayed in restricted mode as it is not the current version. There may be differences and errors due to this.

What is Scala?

Scala is a modern language not unlike Groovy.
It is said to be more powerful (and faster) than Groovy or Rails which has been the reason for its adoption at sites like Twitter.

Many of its features and paradigms favor multi-threaded and concurrent development which may not be unfamiliar to Objective-C and WebObjects developers. Here's a summary:

                             Objective-C                                                                                Java           Scala                                          
 Mutable/Immuable Datatypes  Collections e.g: NSArray/NSMutableArray                                                    No             All datatypes                                 
 Closures                        Blocks (Extension)                                                                     No             Built-in                                      
 Static variables                 Yes                                                                                     Yes             No                                             
 Static methods/functions         Yes                                                                                     Yes             No                                             
 Concurrency                 Grand Central Dispatch (Extension)  Threads      Actors
                              Weakly Typed                                                                            Strongly Typed Strongly Typed                                 

Other notable features include:

                             Objective-C                                 Java     Scala                       
 Parametered methods         Yes e.g: addObject: to:                   No          Yes e.g: add(object= ,to=) 
 Class composition           Categories                                  Interfaces  Traits                       

A fuller description of Scala can be found here.

Why Use Scala?

With Web 2.0, building concurrent WebObjects applications is a must.
Developing and maintaining a concurrent or multi-threaded WebObjects application can be challenging.

Scala offers concurrency that is (effectively) built-in to the language and is inherently thread-safe.
So it may offer new solutions for concurrency in WebObjects.

Can WebObjects be Programmed In Scala?

Yes. It is very simple.
Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward.

WebObjects In Scala

The following is an in-depth explanation of differences between Java and Scala in WebObjects:

EOs in Scala

Thread-Safe Shared Constants and Vars

Scala doesn't have static variables or methods. However, every class does have a Companion Object that will allow you to achieve something equivalent to static variables.
One of the advantages of this approach is that it is thread-safe, so you don't have to worry about synchronizing access to these fields in a concurrent application.

In Java:


public class _Talent extends EOGenericRecord {
public static final String ENTITY_NAME = "Talent";

In Scala:


object _Talent extends  EOGenericRecord {
val ENTITY_NAME = "Talent"

Compacted imports

Two lines in Java is compacted into one in Scala.

In Java:


import com.webobjects.eocontrol.EOGenericRecord;
import com.webobjects.eocontrol.EORelationshipManipulation;

In Scala:


import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation}

WOComponents in Scala

Compact Constructors

Scala allows for simpler use of multi-valued constructors than Java.

In Java:


public class MenuHeader extends WOComponent {

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

In Scala:


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

Simplified Exception Handling

Scala doesn't force you to catch exceptions unlike in Java.
In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions.

In Java:


try {
    EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session());
    epi.setNextPage(context().page());
    nextPage = (WOComponent) epi;
} catch (IllegalArgumentException e) {
    ErrorPageInterface epf = D2W.factory().errorPage(session());
    epf.setMessage(e.toString());
    epf.setNextPage(context().page());
    nextPage = (WOComponent) epf;
}

In Scala:


try {
     
var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session)
     
epi.setNextPage(context.page)
     
nextPage = epi.asInstanceOf[WOComponent]
} catch {
     
case e: IllegalArgumentException => {
            
var epf: ErrorPageInterface = D2W.factory.errorPage(session)
            
epf.setMessage(e.toString)
            
epf.setNextPage(context.page)
            
nextPage = epf.asInstanceOf[WOComponent]
     
}
}

How to Add Scala to a WO Project

Failed to execute the [include] macro. Cause: [You must specify a 'reference' parameter pointing to the entity to include.]. Click on this message for details.

Warning

Note

This is for Eclipse/WOLips IDE

WO Scala Example

The following is a mixed Java/Scala version of the WO Movies D2W app.
All the EO logic and WO components are in Scala.
Only the Application class is Java.

Unknown macro: attachments. Click on this message for details.

Setup

  1. Install and run the OpenBase OBMovies database.
  2. Right-click on Application.java and run as a WOApplication (as usual).

EO Templates

See: Scala templates

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

  1. Change the File Names Extension to "scala"
  2. Uncheck Java under Options