Changes for page WebObjects with Scala
Last modified by Ravi Mendis on 2011/05/10 02:10
From version 428.1
edited by Ravi Mendis
on 2010/01/14 22:54
on 2010/01/14 22:54
Change comment:
There is no comment for this version
To version 435.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 (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,40 +1,34 @@ 1 1 === What is Scala? === 2 2 3 -Scala is a modern language not unlike Groovy. 4 -It is said to be more powerful and faster than Groovy or Rub. 5 -This has been the reason for its adoption at sites like Twitter. 3 +[[Scala>>http://en.wikipedia.org/wiki/Scala_(programming_language)]] is a language for concurrent computing. 4 +In this day and age of multi-core processors, concurrent computing can't be ignored. 6 6 7 -Many of its features andparadigmsfavor multi-threadingandconcurrency.8 - It could besaidthat Scala was designedfrom theground upforconcurrency.6 +Many of Scala's features have been designed with concurrency in mind. 7 +Some of these may not be unfamiliar to Objective-C or WebObjects developers. 9 9 10 - Someof thesemaynot beunfamiliar to Objective-Cand WebObjectsdevelopers. Here's a summary:9 +Here's a quick summary: 11 11 12 -|= 13 -|= Mutable /Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray//14 -|= Closures 15 -|= Static variables 16 -|= Static methods /functions17 -|= Concurrency 18 -|= 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 19 19 20 20 Other notable features include: 21 21 22 -|= 23 -|= Parametered methods 24 -|= Class composition 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 25 25 26 -A fuller description of Scala can be found [[here>>http://en.wikipedia.org/wiki/Scala_(programming_language)]]. 27 - 28 28 === Why Use Scala? === 29 29 30 - WithWeb 2.0, building concurrentWebObjectsapplicationsisamust.31 - Developing and maintaininga concurrentor multi-threadedWebObjects applicationcanbechallenging.27 +Scala is inherently thread-safe. 28 +It has concurrency that is effectively built-in to the language. 32 32 33 -Scala offers concurrency that is (effectively) built-in to the language and is inherently thread-safe. 34 -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. 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). 35 35 36 -In addition Scala offers itself as a solution for tasks that typically would have involved threads in a WebObjects application 37 - 38 38 === Can WebObjects be Programmed In Scala? === 39 39 40 40 Yes. It is very simple. ... ... @@ -46,11 +46,12 @@ 46 46 47 47 == EOs in Scala == 48 48 49 -=== Thread-Safe Shared Vars 43 +=== Thread-Safe Shared Vars === 50 50 51 -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. 52 -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. 53 53 47 +So you don't have to worry about synchronizing access to shared mutable fields in a concurrent application. 48 + 54 54 The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java. 55 55 56 56 Java: ... ... @@ -71,6 +71,14 @@ 71 71 72 72 {{/code}} 73 73 69 +This value will be accessed exactly the same way in both languages: 70 + 71 +{{code}} 72 + 73 +Talent.ENTITY_NAME 74 + 75 +{{/code}} 76 + 74 74 ==== Compacted imports ==== 75 75 76 76 Two lines in Java are compacted into one in Scala. ... ... @@ -121,7 +121,7 @@ 121 121 ==== Simplified Exception Handling ==== 122 122 123 123 Scala doesn't force you to catch exceptions unlike in Java. 124 -In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions.127 +In addition, the syntax employs Scala's very powerful **pattern matching** to handle exceptions. 125 125 126 126 In Java: 127 127 ... ... @@ -159,13 +159,13 @@ 159 159 160 160 {{/code}} 161 161 162 -==== Scala Annotations vs. Gener icAccessors ====165 +==== Scala Annotations vs. Generated Accessors ==== 163 163 164 164 An example of accessing variables in WebObjects with the following languages: 165 165 166 -|= 167 -|= 168 -|= 169 +|= |= Objective-C |= Java |= Scala 170 +|= getter | ##object name## | ##object.name()## | ##object.name## 171 +|= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName## 169 169 170 170 Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention. 171 171 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. ... ... @@ -175,6 +175,8 @@ 175 175 176 176 {{code}} 177 177 181 +import scala.reflect.BeanProperty 182 + 178 178 @BeanProperty var username = new String() 179 179 @BeanProperty var password = new String() 180 180 @BeanProperty var isAssistantCheckboxVisible = false ... ... @@ -183,7 +183,7 @@ 183 183 184 184 == How to Use Scala Collections with EOF == 185 185 186 -One of the benefits of Scala is its very powerful, concurrency-ready collection classes - primarily ##List##, ##Map## ,##Seq##and ##Set##.191 +One of the benefits of Scala is its very powerful, concurrency-ready collection classes - primarily ##List##, ##Map## and ##Set##. 187 187 Employing these instead of ##NSArray## and ##NSDictionary## in WebObjects/EOF may be challenging. 188 188 189 189 But one may modify the EO templates to produce API such as: ... ... @@ -190,21 +190,25 @@ 190 190 191 191 {{code}} 192 192 193 -def movies: NSArray[EOGenericRecord] = { 194 - storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[EOGenericRecord]] 198 +import scala.collection.JavaConversions._ 199 + 200 +def movies = { 201 + storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[Movie]] 195 195 } 196 - 197 -def moviesList :List[EOGenericRecord]= {198 - movies. objects.toList203 + 204 +def moviesList = { 205 + movies.asInstanceOf[java.lang.Iterable[Movie]].toList 199 199 } 200 200 201 201 {{/code}} 202 202 210 +This employs a feature of Scala known as **implicit conversions** to automagically convert a NSArray (a Java Iterable) into a Scala Iterable. 211 + 203 203 == How to Add Scala to a WO Project == 204 204 205 205 {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}} 206 206 207 -{{note title="Note"}}216 +{{note}} 208 208 209 209 This is for Eclipse/WOLips IDE 210 210 ... ... @@ -214,7 +214,7 @@ 214 214 215 215 The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app: 216 216 All the EO logic and WO components are in Scala. 217 -Only the Application class is Java. 226 +Only the Application class remains Java. 218 218 219 219 It is based on the D2W Movies example. 220 220 ... ... @@ -222,15 +222,14 @@ 222 222 223 223 === Setup === 224 224 225 -1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]] 226 -1. Install and start the OpenBase OBMovies database. 234 +1. [[Install the Scala eclipse IDE>>http://www.scala-ide.org/]] 227 227 1. Right-click on Application.java and run as a WOApplication (as usual). 228 228 229 -== ==EO Templates ====237 +== EO Templates == 230 230 231 231 When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor: 232 232 233 -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## 234 234 1. Change the File Names Extension to "scala" 235 235 1. In Destination Paths set the Superclass Package (e.g: base) 236 236 1. Uncheck Java under Options