Changes for page WebObjects with Scala

Last modified by Ravi Mendis on 2011/05/10 02:10

From version 517.1
edited by Ravi Mendis
on 2010/04/06 18:25
Change comment: There is no comment for this version
To version 514.1
edited by Ravi Mendis
on 2010/08/12 03:17
Change comment: Bump section header

Summary

Details

Page properties
Content
... ... @@ -8,19 +8,19 @@
8 8  
9 9  Here's a quick summary:
10 10  
11 -|= |= Objective-C |= Java |= Scala
12 -|= Separation of Mutable & Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | Yes
13 -|= Closures | Blocks (//Extension//) | No | Anonymous Functions
14 -|= Static variables | Yes | Yes | No
15 -|= Static methods or functions | Yes | Yes | No
16 -|= Concurrency | [[Grand Central Dispatch>>http://en.wikipedia.org/wiki/Grand_Central_Dispatch]] (//Extension//)| //Threads// |[[Actors>>http://en.wikipedia.org/wiki/Actor_model]]
17 -|= |= Weakly Typed |=--Strongly Typed--|= Strongly Typed
11 +|= |= Objective-C |= Java |= Scala
12 +|= Separation of Mutable & Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | Yes
13 +|= Closures | Blocks (//Extension//) | No | Anonymous Functions
14 +|= Static variables | Yes | Yes | No
15 +|= Static methods or functions | Yes | Yes | No
16 +|= Concurrency | [[Grand Central Dispatch>>http://en.wikipedia.org/wiki/Grand_Central_Dispatch]] (//Extension//) | //Threads// | [[Actors>>http://en.wikipedia.org/wiki/Actor_model]]
17 +|= |= Weakly Typed |= --Strongly Typed-- |= Strongly Typed
18 18  
19 19  Other notable features include:
20 20  
21 -|= |= Objective-C |= Java |= Scala
22 -|= Parametered methods | Yes //e.g: addObject: to~:// | No | Yes //e.g: add(object= ,to=)//
23 -|= Class composition | Categories | Interfaces | Traits
21 +|= |= Objective-C |= Java |= Scala
22 +|= Parametered methods | Yes //e.g: addObject: to~:// | No | Yes //e.g: add(object= ,to=)//
23 +|= Class composition | Categories | Interfaces | Traits
24 24  
25 25  === Why Use Scala? ===
26 26  
... ... @@ -40,11 +40,12 @@
40 40  
41 41  == EOs in Scala ==
42 42  
43 -=== Thread-Safe Shared Vars ===
43 +=== Thread-Safe Shared Vars ===
44 44  
45 -Scala doesn't have static variables or methods. However, a class can have a //Companion Object// that will allow you to achieve something equivalent to static variables.
46 -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.
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 47  
47 +So you don't have to worry about synchronizing access to shared mutable fields in a concurrent application.
48 +
48 48  The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java.
49 49  
50 50  Java:
... ... @@ -165,9 +165,9 @@
165 165  
166 166  An example of accessing variables in WebObjects with the following languages:
167 167  
168 -|= |= Objective-C |= Java |= Scala
169 -|= getter | ##object name## | ##object.name()## | ##object.name##
170 -|= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName##
169 +|= |= Objective-C |= Java |= Scala
170 +|= getter | ##object name## | ##object.name()## | ##object.name##
171 +|= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName##
171 171  
172 172  Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention.
173 173  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.
... ... @@ -194,16 +194,20 @@
194 194  
195 195  {{code}}
196 196  
197 -def movies: NSArray[Studio] = {
198 - storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[Studio]]
198 +import scala.collection.JavaConversions._
199 +
200 +def movies = {
201 + storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[Movie]]
199 199  }
200 -
201 -def moviesList: List[Studio] = {
202 - movies.objects.toList
203 +
204 +def moviesList = {
205 + movies.asInstanceOf[java.lang.Iterable[Movie]].toList
203 203  }
204 204  
205 205  {{/code}}
206 206  
210 +This employs a feature of Scala known as **implicit conversions** to automagically convert a NSArray (a Java Iterable) into a Scala Iterable.
211 +
207 207  == How to Add Scala to a WO Project ==
208 208  
209 209  {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}}
... ... @@ -226,15 +226,14 @@
226 226  
227 227  === Setup ===
228 228  
229 -1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]]
230 -1. Install and start the OpenBase OBMovies database.
234 +1. [[Install the Scala eclipse IDE>>http://www.scala-ide.org/]]
231 231  1. Right-click on Application.java and run as a WOApplication (as usual).
232 232  
233 -==== EO Templates ====
237 +== EO Templates ==
234 234  
235 235  When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor:
236 236  
237 -1. Point to the local [[Scala versions>>http://wiki.objectstyle.org/confluence/display/WOL/EOGenerator+Templates+and+Additions]] of the .eotemplate files for ##Entity## and ##//Entity//##
241 +1. Point to the local [[Scala versions>>http://wiki.objectstyle.org/confluence/display/WOL/EOGenerator+Templates+and+Additions]] of the .eotemplate files for ##Entity## and ##Entity##
238 238  1. Change the File Names Extension to "scala"
239 239  1. In Destination Paths set the Superclass Package (e.g: base)
240 240  1. Uncheck Java under Options
... ... @@ -245,11 +245,3 @@
245 245  1. Set ##scala.home## (the location Scala has been installed onto) in the project ##build.properties## file
246 246  1. [[Add the scalac task and properties>>Configuring Ant to Build Scala with WebObjects]] to the ant build.xml file
247 247  1. Run from the project directory: ##sudo ant clean install##
248 -
249 -== Caveats ==
250 -
251 -{{warning}}
252 -
253 -Currently mixed Scala and Java projects aren't supported by the Scala Eclipse IDE, though it is possible to do so providing your project is either mostly Java or mostly Scala.
254 -
255 -{{/warning}}