Changes for page WebObjects with Scala
Last modified by Ravi Mendis on 2011/05/10 02:10
From version 437.1
edited by John Huss
on 2010/12/03 16:40
on 2010/12/03 16:40
Change comment:
There is no comment for this version
To 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
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. johnthuss1 +XWiki.rmendis - Content
-
... ... @@ -1,9 +1,9 @@ 1 1 === What is Scala? === 2 2 3 -[[Scala>>http://en.wikipedia.org/wiki/Scala_(programming_language)]] is a JVMlanguagethat is a hybrid ofObject-Oriented and Functional styles. It is useful as complete generalpurpose replacement for Java. But its built-in Actors library makes it especially attractive for concurrent computing.3 +[[Scala>>http://en.wikipedia.org/wiki/Scala_(programming_language)]] is a language 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 , primarily a preference for immutability and the use of other functional language paradigms.6 +Many of Scala's features have been designed with concurrency in mind. 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 canhelpyouto write thread-safecode.28 -It has concurrency that is built-in to the standard library, primarily via Actors.27 +Scala is inherently thread-safe. 28 +It has concurrency that is effectively built-in to the language. 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 ... ... @@ -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. Is this true? I don't think Scala "object" instances (with the object keyword) are guaranteed to be thread-safe; they are just singletons45 +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. 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,10 +188,27 @@ 188 188 189 189 == How to Use Scala Collections with EOF == 190 190 191 -To use the Scala Collections API with an NSArray or NSDictionary you simply need to add an import:import scala.collection.JavaConversions. 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. 192 192 193 - Then youcanaccessthe typicalScala collectionmethods directlyon NSArray. Thisemploysafeatureof Scala known as implicitconversions toautomagicallycast a NSArray (a Java Iterable) intoa Scala Iterable while leaving the actual object unchanged.lternatively,yououldgenerate an actual newscala.List instance by calling myNSArray.toList.194 +But one may modify the EO templates to produce API such as: 194 194 196 +{{code}} 197 + 198 +import scala.collection.JavaConversions._ 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 +{{/code}} 209 + 210 +This employs a feature of Scala known as **implicit conversions** to automagically convert a NSArray (a Java Iterable) into a Scala Iterable. 211 + 195 195 == How to Add Scala to a WO Project == 196 196 197 197 {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}} ... ... @@ -217,8 +217,6 @@ 217 217 1. [[Install the Scala eclipse IDE>>http://www.scala-ide.org/]] 218 218 1. Right-click on Application.java and run as a WOApplication (as usual). 219 219 220 -Application can be made into a Scala class as well, but then you will have to create a launcher in Eclipse manually. 221 - 222 222 == EO Templates == 223 223 224 224 When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor: