What is Scala?
Scala is a modern language not unlike Groovy.
It is said to be more powerful (and faster) than Groovy or Ruby which has been the reason for its adoption at sites like Twitter.Many of its features and paradigms favor multi-threading and concurrency. Some of these , multi-paradigm JVM language that is most often compared to Groovy, Clojure and Erlang. Its functional language foundations and built-in Actors library 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 not be ignored. Many of the design features of Scala have been chosen with concurrency in mind, some of which may not be unfamiliar to Objective-C and or WebObjects developers. Here's a summary:
| Objective-C | Java | Scala | |
---|---|---|---|---|
Mutable/Immuable Datatypes | Collections 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 /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 | Traits |
...
Yes - via Traits |
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.
In other words, developing Ajax (i.e asynchronous communication) with WO will require concurrent request handling and thread-safe code, for which Scala is a better choice than Java.
In addition it may offer new solutions for concurrency in WebObjects and EOFFor WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for concurrent computing. (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:
...
Scala doesn't have static variables or methods. However, Instead Scala employs the 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. One of the advantages of this approach is that it is thread-safe, so you
You don't have to worry about synchronizing access to these 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.
Java:
Code Block | ||||
---|---|---|---|---|
| ||||
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 | ||||
---|---|---|---|---|
| ||||
import com.webobjects.eocontrol.EOGenericRecord; import com.webobjects.eocontrol.EORelationshipManipulation; |
...
Scala allows for simpler use of multi-valued constructors than Java.
In Java:
Code Block | ||||
---|---|---|---|---|
| ||||
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 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:
Code Block | ||||
---|---|---|---|---|
| ||||
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; } |
...
Code Block |
---|
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 Use Scala Collections with EOF
One of the benefits of Scala is its very powerful, concurrency-ready collection classes - primarily List
, Map
, Seq
and Set
.
Employing these instead of NSArray
and NSDictionary
in WebObjects/EOF may be challenging.
But one may modify the EO templates to produce API such as:
...
Scala Annotations vs. Generated Accessors
Here's an example of accessing variables in the following languages:
| Objective-C | Java | Scala |
---|---|---|---|
getter | | | |
setter | | | |
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.
E.g, in Main.scala we annotate our component keys with @BeanProperty
to automatically create public "set" and "get" methods.
These variables can then be accessed via KVC.
Code Block |
---|
import scala.reflect.BeanProperty
@BeanProperty var username = new String()
@BeanProperty var password = new String()
@BeanProperty var isAssistantCheckboxVisible = false
|
How to Use Scala Collections with EOF
To use the Scala Collections API with an NSArray or NSDictionary you simply need to add an import:
Code Block | ||||
---|---|---|---|---|
| ||||
import scala.collection.JavaConversions._
|
After that, you 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.
How to Add Scala to a WO Project (in Eclipse)
Include Page | ||||
---|---|---|---|---|
|
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 is remains Java.
It is based on the D2W Movies example.
Attachments | ||
---|---|---|
|
Setup
- Install the Scala eclipse IDE
- Install and start the OpenBase OBMovies database.
- 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:
- Point to the local Scala versions of the .eotemplate files for
Entity
and_Entity
- Change the File Names Extension to "scala"
- In Destination Paths set the Superclass Package (e.g: base)
- Uncheck Java under Options
How to Build & Deploy a WebObjects Scala Project with Ant
- Download and install Scala
- Set
scala.home
(the location Scala has been installed onto) in the projectbuild.properties
file - Add the scalac task and properties to the ant build.xml file
- Run from the project directory:
sudo ant clean install