Changes for page WebObjects with Scala

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

From version 532.1
edited by Ravi Mendis
on 2010/08/10 05:51
Change comment: There is no comment for this version
To version 533.1
edited by Ravi Mendis
on 2011/05/10 02:10
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,39 +1,118 @@
1 1  === What is Scala? ===
2 2  
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.
3 +[[Scala>>url:http://www.scala-lang.org/||shape="rect"]] is a modern, multi-paradigm JVM language that is most often compared to [[Groovy>>url:http://en.wikipedia.org/wiki/Groovy_(programming_language)||shape="rect"]], [[Clojure>>url:http://en.wikipedia.org/wiki/Clojure||shape="rect"]] and [[Erlang>>url:http://en.wikipedia.org/wiki/Erlang_(programming_language)||shape="rect"]]. Its [[functional language>>url:http://en.wikipedia.org/wiki/Functional_programming||shape="rect"]] foundations and built-in [[Actors>>url:http://en.wikipedia.org/wiki/Actor_model||shape="rect"]] library make it especially attractive for concurrent computing. (Scala is an abbreviation for "scalable" hinting at its design goals).
5 5  
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.
5 +In this day and age of multi-core processors concurrent computing can not be ignored. Many of the design features of Scala have been chosen with concurrency in mind, some of which may not be unfamiliar to Objective-C or WebObjects developers. Here's a summary:
8 8  
9 -Here's a quick summary:
7 +|=(((
8 +
9 +)))|=(((
10 +Objective-C
11 +)))|=(((
12 +Java
13 +)))|=(((
14 +Scala
15 +)))
16 +|=(((
17 +Immutability
18 +)))|(((
19 +Partial - via collections //e.g: NSArray/NSMutableArray//
20 +)))|(((
21 +No
22 +)))|(((
23 +Yes
24 +)))
25 +|=(((
26 +Closures
27 +)))|(((
28 +Yes - via Blocks (//Extension//)
29 +)))|(((
30 +No
31 +)))|(((
32 +Yes - via Anonymous Functions
33 +)))
34 +|=(((
35 +Static variables
36 +)))|(((
37 +Yes
38 +)))|(((
39 +Yes
40 +)))|(((
41 +No
42 +)))
43 +|=(((
44 +Static methods
45 +)))|(((
46 +Yes
47 +)))|(((
48 +Yes
49 +)))|(((
50 +No
51 +)))
52 +|=(((
53 +Concurrency
54 +)))|(((
55 +Yes - viaĀ [[Grand Central Dispatch>>url:http://en.wikipedia.org/wiki/Grand_Central_Dispatch||shape="rect"]] (//Extension//)
56 +)))|(((
57 +//Yes - via Threads//
58 +)))|(((
59 +Yes - viaĀ [[Actors>>url:http://en.wikipedia.org/wiki/Actor_model||shape="rect"]]
60 +)))
61 +|=(((
62 +
63 +)))|=(((
64 +Weakly Typed
65 +)))|=(((
66 +(% style="text-decoration: line-through;" %)Strongly Typed
67 +)))|=(((
68 +Strongly Typed
69 +)))
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
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
73 +|=(((
74 +
75 +)))|=(((
76 +Objective-C
77 +)))|=(((
78 +Java
79 +)))|=(((
80 +Scala
81 +)))
82 +|=(((
83 +Parametered methods
84 +)))|(((
85 +Yes //e.g: addObject: to~://
86 +)))|(((
87 +No
88 +)))|(((
89 +Yes //e.g: add(object= ,to=)//
90 +)))
91 +|=(((
92 +Class composition
93 +)))|(((
94 +Yes - via Categories
95 +)))|(((
96 +Yes - via Interfaces
97 +)))|(((
98 +Yes - via [[Traits>>url:http://en.wikipedia.org/wiki/Trait_(computer_science)||shape="rect"]]
99 +)))
24 24  
25 25  === Why Use Scala? ===
26 26  
27 -Scala is inherently thread-safe.
28 -It has concurrency that is effectively built-in to the language.
103 +For WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for [[concurrent computing>>doc:Building Concurrent Applications with WebObjects and Scala]]. (In other words, Scala Actors can be used for problems that would have normally required threads).
29 29  
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 -
32 32  === Can WebObjects be Programmed In Scala? ===
33 33  
34 34  Yes. It is very simple.
35 -Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward.
108 + By virtue of being a JVM-language, Scala compiles to java bytecode.
36 36  
110 +Furthermore, being a multi-paradigm language grants Scala easy WebObjects-interoperability.
111 +
112 +===== Caveats =====
113 +
114 +Legacy tool support is often cited as a weak point. The [[Eclipse Scala plugin>>url:http://www.scala-ide.org||shape="rect"]] has been found to be slow at times and sometimes buggy.
115 +
37 37  = WebObjects In Scala =
38 38  
39 39  The following highlights some of the differences between Java and Scala in WebObjects:
... ... @@ -42,18 +42,20 @@
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.
124 +Scala doesn't have static variables or methods. Instead Scala employs the [[Singleton Pattern>>url:http://en.wikipedia.org/wiki/Singleton_pattern||shape="rect"]] 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 -So you don't have to worry about synchronizing access to shared mutable fields in a concurrent application.
126 +You don't have to worry about synchronizing access to shared mutable fields in a concurrent application.
127 + (This is not however true when for example you have a {{code language="none"}}val{{/code}} declared as a {{code language="none"}}NSMutableArray{{/code}}. You will still have to synchronize when adding to or removing from this mutable field).
48 48  
49 49  The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java.
50 50  
51 51  Java:
52 52  
53 -{{code value="java"}}
133 +{{code 0="java"}}
54 54  
55 -public class _Talent extends EOGenericRecord {
135 +public class Talent extends EOGenericRecord {
56 56   public static final String ENTITY_NAME = "Talent";
137 +}
57 57  
58 58  {{/code}}
59 59  
... ... @@ -61,8 +61,9 @@
61 61  
62 62  {{code}}
63 63  
64 -object Talent extends EOGenericRecord {
145 +object Talent {
65 65   val ENTITY_NAME = "Talent"
147 +}
66 66  
67 67  {{/code}}
68 68  
... ... @@ -76,11 +76,9 @@
76 76  
77 77  ==== Compacted imports ====
78 78  
79 -Two lines in Java are compacted into one in Scala.
80 -
81 81  In Java:
82 82  
83 -{{code value="java"}}
163 +{{code 0="java"}}
84 84  
85 85  import com.webobjects.eocontrol.EOGenericRecord;
86 86  import com.webobjects.eocontrol.EORelationshipManipulation;
... ... @@ -103,7 +103,7 @@
103 103  
104 104  In Java:
105 105  
106 -{{code value="java"}}
186 +{{code 0="java"}}
107 107  
108 108  public class MenuHeader extends WOComponent {
109 109  
... ... @@ -110,6 +110,7 @@
110 110   public MenuHeader(WOContext aContext) {
111 111   super(aContext);
112 112   }
193 +}
113 113  
114 114  {{/code}}
115 115  
... ... @@ -117,7 +117,7 @@
117 117  
118 118  {{code}}
119 119  
120 -class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) {
201 +class MenuHeader(context: WOContext) extends WOComponent(context: WOContext)
121 121  
122 122  {{/code}}
123 123  
... ... @@ -124,11 +124,11 @@
124 124  ==== Simplified Exception Handling ====
125 125  
126 126  Scala doesn't force you to catch exceptions unlike in Java.
127 -In addition, the syntax employs Scala's very powerful **pattern matching** to handle exceptions.
208 + In addition, the syntax employs Scala's very powerful **pattern matching** to handle exceptions.
128 128  
129 129  In Java:
130 130  
131 -{{code value="java"}}
212 +{{code 0="java"}}
132 132  
133 133  try {
134 134   EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session());
... ... @@ -164,17 +164,41 @@
164 164  
165 165  ==== Scala Annotations vs. Generated Accessors ====
166 166  
167 -An example of accessing variables in WebObjects with the following languages:
248 +Here's an example of accessing variables in the following languages:
168 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##
250 +|=(((
251 +
252 +)))|=(((
253 +Objective-C
254 +)))|=(((
255 +Java
256 +)))|=(((
257 +Scala
258 +)))
259 +|=(((
260 +getter
261 +)))|(((
262 +{{code language="none"}}[WO:object name]{{/code}}
263 +)))|(((
264 +{{code language="none"}}object.name(){{/code}}
265 +)))|(((
266 +{{code language="none"}}object.name{{/code}}
267 +)))
268 +|=(((
269 +setter
270 +)))|(((
271 +{{code language="none"}}[object setName:aName]{{/code}}
272 +)))|(((
273 +{{code language="none"}}object.setName(aName){{/code}}
274 +)))|(((
275 +{{code language="none"}}object.name = aName{{/code}}
276 +)))
172 172  
173 173  Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention.
174 -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.
279 + 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 175  
176 -E.g, in Main.scala we annotate our component keys with ##@BeanProperty## to automatically create public "set" and "get" methods.
177 -These variables can then be accessed via //KVC//.
281 +E.g, in Main.scala we annotate our component keys with {{code language="none"}}@BeanProperty{{/code}} to automatically create public "set" and "get" methods.
282 + These variables can then be accessed via //KVC//.
178 178  
179 179  {{code}}
180 180  
... ... @@ -188,56 +188,44 @@
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.
296 +To use the Scala Collections API with an NSArray or NSDictionary you simply need to add an import:
193 193  
194 -But one may modify the EO templates to produce API such as:
298 +{{code 0="java"}}
195 195  
196 -{{code}}
197 -
198 198  import scala.collection.JavaConversions._
199 199  
200 -def movies: NSArray[Movie] = {
201 - storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[Movie]]
202 -}
203 -
204 -def moviesList: Iterable[Movie] = {
205 - movies.asInstanceOf[java.lang.Iterable[Movie]]
206 -}
207 -
208 208  {{/code}}
209 209  
210 -== How to Add Scala to a WO Project ==
304 +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.
211 211  
212 -{{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}}
306 +== How to Add Scala to a WO Project (in Eclipse) ==
213 213  
214 -{{note}}
308 +{{include 0="WOL:Adding Scala Support to a WOLips Project"/}}
215 215  
216 -This is for Eclipse/WOLips IDE
217 -
218 -{{/note}}
219 -
220 220  == WO Scala Example ==
221 221  
222 222  The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app:
223 -All the EO logic and WO components are in Scala.
224 -Only the Application class is Java.
313 + All the EO logic and WO components are in Scala.
314 + Only the Application class remains Java.
225 225  
226 226  It is based on the D2W Movies example.
227 227  
228 -{{attachments patterns=".*zip"}}{{/attachments}}
318 +{{attachments patterns=".*zip"/}}
229 229  
230 230  === Setup ===
231 231  
232 -1. [[Install the Scala eclipse IDE>>http://www.scala-ide.org/]]
233 -1. Install and start the OpenBase OBMovies database.
322 +1. [[Install the Scala eclipse IDE>>url:http://download.scala-ide.org/||shape="rect"]]
234 234  1. Right-click on Application.java and run as a WOApplication (as usual).
235 235  
236 -==== EO Templates ====
325 +{{note}}
326 +Application can be made into a Scala class as well, but then you will have to create a launcher in Eclipse manually.
327 +{{/note}}
237 237  
238 -When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor:
329 +== EO Templates ==
239 239  
240 -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##
331 +When you create your {{code language="none"}}.eogen{{/code}} file, be sure to make the following changes in the EOGenerator Editor:
332 +
333 +1. Point to the local [[Scala versions>>url:http://wiki.objectstyle.org/confluence/display/WOL/EOGenerator+Templates+and+Additions||shape="rect"]] of the .eotemplate files for {{code language="none"}}Entity{{/code}} and {{code language="none"}}_Entity{{/code}}
241 241  1. Change the File Names Extension to "scala"
242 242  1. In Destination Paths set the Superclass Package (e.g: base)
243 243  1. Uncheck Java under Options
... ... @@ -244,15 +244,7 @@
244 244  
245 245  == How to Build & Deploy a WebObjects Scala Project with Ant ==
246 246  
247 -1. [[Download>>http://www.scala-lang.org/downloads]] and install Scala
248 -1. Set ##scala.home## (the location Scala has been installed onto) in the project ##build.properties## file
249 -1. [[Add the scalac task and properties>>Configuring Ant to Build Scala with WebObjects]] to the ant build.xml file
250 -1. Run from the project directory: ##sudo ant clean install##
251 -
252 -== Caveats ==
253 -
254 -{{warning}}
255 -
256 -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.
257 -
258 -{{/warning}}
340 +1. [[Download>>url:http://www.scala-lang.org/downloads||shape="rect"]] and install Scala
341 +1. Set {{code language="none"}}scala.home{{/code}} (the location Scala has been installed onto) in the project {{code language="none"}}build.properties{{/code}} file
342 +1. [[Add the scalac task and properties>>doc:Configuring Ant to Build Scala with WebObjects]] to the ant build.xml file
343 +1. Run from the project directory: {{code language="none"}}sudo ant clean install{{/code}}