Changes for page WebObjects with Scala
Last modified by Ravi Mendis on 2011/05/10 02:10
From version 357.1
edited by Ravi Mendis
on 2009/10/13 23:38
on 2009/10/13 23:38
Change comment:
There is no comment for this version
To version 359.1
edited by Ravi Mendis
on 2010/01/17 19:21
on 2010/01/17 19:21
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,10 +1,13 @@ 1 1 === What is Scala? === 2 2 3 -Scala is a modernlanguagenotunlikeGroovy.4 -I tis saidto bemorepowerful (andfaster)than Groovyor Ruby which has beenthereasonforitsadoption atsiteslike Twitter.3 +Scala is a language for concurrent computing. 4 +In the day and age of multi-core processors, concurrent computing can't be ignored. 5 5 6 -Many of its features and paradigms favor multi-threading and concurrency. Some of these may not be unfamiliar to Objective-C and WebObjects developers. Here's a summary: 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. 7 7 9 +Here's a quick summary: 10 + 8 8 |= |= Objective-C |= Java |= Scala 9 9 |= Mutable/Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | Yes 10 10 |= Closures | Blocks (//Extension//) | No | Anonymous Functions ... ... @@ -23,13 +23,13 @@ 23 23 24 24 === Why Use Scala? === 25 25 26 -With Web 2.0, building concurrent WebObjects applications is a must. 27 27 Developing and maintaining a concurrent or multi-threaded WebObjects application can be challenging. 28 28 29 - Scala offersconcurrencythats (effectively)built-in to thelanguageandis inherently thread-safe.30 -I n otherwords, developing Ajax (i.e asynchronouscommunication)withWO willrequireconcurrentrequest handlingdthread-safecode,forwhichScala is a better choice than Java.31 +The lack of static variables means that Scala is inherently thread-safe. 32 +It has concurrency that is effectively built-in to the language in the form of Actors. 31 31 32 -In addition it may offer new solutions for concurrency in WebObjects and EOF. 34 +So for WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for concurrent applications. 35 +(In other words, Scala can be used for problems that would normally have required threads). 33 33 34 34 === Can WebObjects be Programmed In Scala? === 35 35 ... ... @@ -47,11 +47,11 @@ 47 47 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. 48 48 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. 49 49 50 -The following is an example of the use of a //Companion Object// for Talent in Scala instead of static fields in Java. 53 +The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java. 51 51 52 52 Java: 53 53 54 -{{code}} 57 +{{code value="java"}} 55 55 56 56 public class _Talent extends EOGenericRecord { 57 57 public static final String ENTITY_NAME = "Talent"; ... ... @@ -62,7 +62,7 @@ 62 62 63 63 {{code}} 64 64 65 -object _Talent extends68 +object Talent extends EOGenericRecord { 66 66 val ENTITY_NAME = "Talent" 67 67 68 68 {{/code}} ... ... @@ -73,7 +73,7 @@ 73 73 74 74 In Java: 75 75 76 -{{code}} 79 +{{code value="java"}} 77 77 78 78 import com.webobjects.eocontrol.EOGenericRecord; 79 79 import com.webobjects.eocontrol.EORelationshipManipulation; ... ... @@ -96,7 +96,7 @@ 96 96 97 97 In Java: 98 98 99 -{{code}} 102 +{{code value="java"}} 100 100 101 101 public class MenuHeader extends WOComponent { 102 102 ... ... @@ -121,7 +121,7 @@ 121 121 122 122 In Java: 123 123 124 -{{code}} 127 +{{code value="java"}} 125 125 126 126 try { 127 127 EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session()); ... ... @@ -155,6 +155,28 @@ 155 155 156 156 {{/code}} 157 157 161 +==== Scala Annotations vs. Generic Accessors ==== 162 + 163 +An example of accessing variables in WebObjects with the following languages: 164 + 165 +|= |= Objective-C |= Java |= Scala 166 +|= getter | ##object name## | ##object.name()## | ##object.name## 167 +|= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName## 168 + 169 +Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention. 170 +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. 171 + 172 +E.g, in Main.scala we annotate our component keys with ##@BeanProperty## to automatically create public "set" and "get" methods. 173 +These variables can then be accessed via //KVC//. 174 + 175 +{{code}} 176 + 177 +@BeanProperty var username = new String() 178 +@BeanProperty var password = new String() 179 +@BeanProperty var isAssistantCheckboxVisible = false 180 + 181 +{{/code}} 182 + 158 158 == How to Use Scala Collections with EOF == 159 159 160 160 One of the benefits of Scala is its very powerful, concurrency-ready collection classes - primarily ##List##, ##Map##, ##Seq## and ##Set##. ... ... @@ -206,4 +206,12 @@ 206 206 207 207 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//## 208 208 1. Change the File Names Extension to "scala" 234 +1. In Destination Paths set the Superclass Package (e.g: base) 209 209 1. Uncheck Java under Options 236 + 237 +== How to Build & Deploy a WebObjects Scala Project with Ant == 238 + 239 +1. [[Download>>http://www.scala-lang.org/downloads]] and install Scala 240 +1. Set ##scala.home## (the location Scala has been installed onto) in the project ##build.properties## file 241 +1. [[Add the scalac task and properties>>Configuring Ant to Build Scala with WebObjects]] to the ant build.xml file 242 +1. Run from the project directory: ##sudo ant clean install##