Wiki source code of WebObjects with Scala

Version 533.1 by Ravi Mendis on 2011/05/10 02:10

Show last authors
1 === What is Scala? ===
2
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).
4
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:
6
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 )))
70
71 Other notable features include:
72
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 )))
100
101 === Why Use Scala? ===
102
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).
104
105 === Can WebObjects be Programmed In Scala? ===
106
107 Yes. It is very simple.
108 By virtue of being a JVM-language, Scala compiles to java bytecode.
109
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
116 = WebObjects In Scala =
117
118 The following highlights some of the differences between Java and Scala in WebObjects:
119
120 == EOs in Scala ==
121
122 === Thread-Safe Shared Vars ===
123
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.
125
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).
128
129 The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java.
130
131 Java:
132
133 {{code 0="java"}}
134
135 public class Talent extends EOGenericRecord {
136 public static final String ENTITY_NAME = "Talent";
137 }
138
139 {{/code}}
140
141 Scala:
142
143 {{code}}
144
145 object Talent {
146 val ENTITY_NAME = "Talent"
147 }
148
149 {{/code}}
150
151 This value will be accessed exactly the same way in both languages:
152
153 {{code}}
154
155 Talent.ENTITY_NAME
156
157 {{/code}}
158
159 ==== Compacted imports ====
160
161 In Java:
162
163 {{code 0="java"}}
164
165 import com.webobjects.eocontrol.EOGenericRecord;
166 import com.webobjects.eocontrol.EORelationshipManipulation;
167
168 {{/code}}
169
170 In Scala:
171
172 {{code}}
173
174 import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation}
175
176 {{/code}}
177
178 == WOComponents in Scala ==
179
180 ==== Compact Constructors ====
181
182 Scala allows for simpler use of multi-valued constructors than Java.
183
184 In Java:
185
186 {{code 0="java"}}
187
188 public class MenuHeader extends WOComponent {
189
190 public MenuHeader(WOContext aContext) {
191 super(aContext);
192 }
193 }
194
195 {{/code}}
196
197 In Scala:
198
199 {{code}}
200
201 class MenuHeader(context: WOContext) extends WOComponent(context: WOContext)
202
203 {{/code}}
204
205 ==== Simplified Exception Handling ====
206
207 Scala doesn't force you to catch exceptions unlike in Java.
208 In addition, the syntax employs Scala's very powerful **pattern matching** to handle exceptions.
209
210 In Java:
211
212 {{code 0="java"}}
213
214 try {
215 EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session());
216 epi.setNextPage(context().page());
217 nextPage = (WOComponent) epi;
218 } catch (IllegalArgumentException e) {
219 ErrorPageInterface epf = D2W.factory().errorPage(session());
220 epf.setMessage(e.toString());
221 epf.setNextPage(context().page());
222 nextPage = (WOComponent) epf;
223 }
224
225 {{/code}}
226
227 In Scala:
228
229 {{code}}
230
231 try {
232 var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session)
233 epi.setNextPage(context.page)
234 nextPage = epi.asInstanceOf[WOComponent]
235 } catch {
236 case e: IllegalArgumentException => {
237 var epf: ErrorPageInterface = D2W.factory.errorPage(session)
238 epf.setMessage(e.toString)
239 epf.setNextPage(context.page)
240 nextPage = epf.asInstanceOf[WOComponent]
241 }
242 }
243
244 {{/code}}
245
246 ==== Scala Annotations vs. Generated Accessors ====
247
248 Here's an example of accessing variables in the following languages:
249
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 )))
277
278 Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention.
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.
280
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//.
283
284 {{code}}
285
286 import scala.reflect.BeanProperty
287
288 @BeanProperty var username = new String()
289 @BeanProperty var password = new String()
290 @BeanProperty var isAssistantCheckboxVisible = false
291
292 {{/code}}
293
294 == How to Use Scala Collections with EOF ==
295
296 To use the Scala Collections API with an NSArray or NSDictionary you simply need to add an import:
297
298 {{code 0="java"}}
299
300 import scala.collection.JavaConversions._
301
302 {{/code}}
303
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.
305
306 == How to Add Scala to a WO Project (in Eclipse) ==
307
308 {{include 0="WOL:Adding Scala Support to a WOLips Project"/}}
309
310 == WO Scala Example ==
311
312 The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app:
313 All the EO logic and WO components are in Scala.
314 Only the Application class remains Java.
315
316 It is based on the D2W Movies example.
317
318 {{attachments patterns=".*zip"/}}
319
320 === Setup ===
321
322 1. [[Install the Scala eclipse IDE>>url:http://download.scala-ide.org/||shape="rect"]]
323 1. Right-click on Application.java and run as a WOApplication (as usual).
324
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}}
328
329 == EO Templates ==
330
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}}
334 1. Change the File Names Extension to "scala"
335 1. In Destination Paths set the Superclass Package (e.g: base)
336 1. Uncheck Java under Options
337
338 == How to Build & Deploy a WebObjects Scala Project with Ant ==
339
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}}