Changes for page WebObjects with Scala
Last modified by Ravi Mendis on 2011/05/10 02:10
From version 436.1
edited by Ravi Mendis
on 2010/09/09 23:13
on 2010/09/09 23:13
Change comment:
There is no comment for this version
To version 439.1
edited by Ravi Mendis
on 2010/12/23 06:45
on 2010/12/23 06:45
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,9 +1,9 @@ 1 1 === What is Scala? === 2 2 3 -[[Scala>>http://en.wikipedia.org/wiki/Scala_(programming_language)]] is a language for concurrent computing. 3 +[[Scala>>http://en.wikipedia.org/wiki/Scala_(programming_language)]] is a JVM language that is a hybrid of Object-Oriented and Functional styles. It is useful as complete general purpose replacement for Java. But its built-in Actors library makes it especially attractive for concurrent computing. 4 4 In this day and age of multi-core processors, concurrent computing can't be ignored. 5 5 6 -Many of Scala's features have been designed with concurrency in mind. 6 +Many of Scala's features have been designed with concurrency in mind, primarily a preference for immutability and the use of other functional language paradigms. 7 7 Some of these may not be unfamiliar to Objective-C or WebObjects developers. 8 8 9 9 Here's a quick summary: ... ... @@ -24,8 +24,8 @@ 24 24 25 25 === Why Use Scala? === 26 26 27 -Scala is inherently thread-safe.28 -It has concurrency that is effectivelybuilt-in to thelanguage.27 +Scala can help you to write thread-safe code. 28 +It has concurrency that is built-in to the standard library, primarily via Actors. 29 29 30 30 So for WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for [[concurrent applications>>Building Concurrent Applications with WebObjects and Scala]]. (In other words, Scala Actors can be used for problems that would have normally required threads). 31 31 ... ... @@ -34,6 +34,8 @@ 34 34 Yes. It is very simple. 35 35 Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward. 36 36 37 +However, tool support is a weak point. You should use Eclipse 3.6 with bundle-less WO builds. If your Application class is in Scala then you will have to create an Eclipse launch configuration manually. One weird thing is that once you add the Scala nature to a WO project WOD completion will stop working - this is unfortunate. Also the Scala plugin is rather slow and still very buggy. 38 + 37 37 = WebObjects In Scala = 38 38 39 39 The following highlights some of the differences between Java and Scala in WebObjects: ... ... @@ -42,7 +42,7 @@ 42 42 43 43 === Thread-Safe Shared Vars === 44 44 45 -Scala 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. 47 +Scala 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 46 46 47 47 So you don't have to worry about synchronizing access to shared mutable fields in a concurrent application. 48 48 ... ... @@ -188,26 +188,15 @@ 188 188 189 189 == How to Use Scala Collections with EOF == 190 190 191 -One of the benefits of Scala is its very powerful, concurrency-ready collection classes - primarily ##List##, ##Map## and ##Set##. 192 -Employing these instead of ##NSArray## and ##NSDictionary## in WebObjects/EOF may be challenging. 193 +To use the Scala Collections API with an NSArray or NSDictionary you simply need to add an import: 193 193 194 - Butone may modify theEO templates to produceAPI suchas:195 +{{code value="java"}} 195 195 196 -{{code}} 197 - 198 198 import scala.collection.JavaConversions._ 199 199 200 -def movies = { 201 - storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[Movie]] 202 -} 203 - 204 -def moviesList = { 205 - movies.asInstanceOf[java.lang.Iterable[Movie]].toList 206 -} 207 - 208 208 {{/code}} 209 209 210 -This employs a feature of Scala known as **implicit conversions**to automagically convert a NSArray (a Java Iterable) into a Scala Iterable.201 +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. Alternatively, you could generate an actual new scala.List instance by calling myNSArray.toList. 211 211 212 212 == How to Add Scala to a WO Project == 213 213 ... ... @@ -234,6 +234,8 @@ 234 234 1. [[Install the Scala eclipse IDE>>http://www.scala-ide.org/]] 235 235 1. Right-click on Application.java and run as a WOApplication (as usual). 236 236 228 +Application can be made into a Scala class as well, but then you will have to create a launcher in Eclipse manually. 229 + 237 237 == EO Templates == 238 238 239 239 When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor: