Wiki source code of WebObjects with Scala
Version 367.1 by Ravi Mendis on 2009/12/02 00:05
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | === What is Scala? === | ||
2 | |||
3 | Scala is a modern language not unlike Groovy. | ||
4 | It is said to be more powerful (and faster) than Groovy or Ruby which has been the reason for its adoption at sites like Twitter. | ||
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: | ||
7 | |||
8 | |= |= Objective-C |= Java |= Scala | ||
9 | |= Mutable/Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | Yes | ||
10 | |= Closures | Blocks (//Extension//) | No | Anonymous Functions | ||
11 | |= Static variables | Yes | Yes | No | ||
12 | |= Static methods/functions | Yes | Yes | No | ||
13 | |= Concurrency | [[Grand Central Dispatch>>http://en.wikipedia.org/wiki/Grand_Central_Dispatch]] (//Extension//)| //Threads// |[[Actors>>http://en.wikipedia.org/wiki/Actor_model]] | ||
14 | |= |= Weakly Typed |=--Strongly Typed--|= Strongly Typed | ||
15 | |||
16 | Other notable features include: | ||
17 | |||
18 | |= |= Objective-C |= Java |= Scala | ||
19 | |= Parametered methods | Yes //e.g: addObject: to~:// | No | Yes //e.g: add(object= ,to=)// | ||
20 | |= Class composition | Categories | Interfaces | Traits | ||
21 | |||
22 | A fuller description of Scala can be found [[here>>http://en.wikipedia.org/wiki/Scala_(programming_language)]]. | ||
23 | |||
24 | === Why Use Scala? === | ||
25 | |||
26 | With Web 2.0, building concurrent WebObjects applications is a must. | ||
27 | Developing and maintaining a concurrent or multi-threaded WebObjects application can be challenging. | ||
28 | |||
29 | Scala offers concurrency that is (effectively) built-in to the language and is inherently thread-safe. | ||
30 | 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. | ||
31 | |||
32 | In addition it may offer new solutions for concurrency in WebObjects and EOF. | ||
33 | |||
34 | === Can WebObjects be Programmed In Scala? === | ||
35 | |||
36 | Yes. It is very simple. | ||
37 | Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward. | ||
38 | |||
39 | = WebObjects In Scala = | ||
40 | |||
41 | The following highlights some of the differences between Java and Scala in WebObjects: | ||
42 | |||
43 | == EOs in Scala == | ||
44 | |||
45 | === Thread-Safe Shared Vars === | ||
46 | |||
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 | 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 | |||
50 | The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java. | ||
51 | |||
52 | Java: | ||
53 | |||
54 | {{code}} | ||
55 | |||
56 | public class Talent extends EOGenericRecord { | ||
57 | public static final String ENTITY_NAME = "Talent"; | ||
58 | |||
59 | {{/code}} | ||
60 | |||
61 | Scala: | ||
62 | |||
63 | {{code}} | ||
64 | |||
65 | object Talent extends EOGenericRecord { | ||
66 | val ENTITY_NAME = "Talent" | ||
67 | |||
68 | {{/code}} | ||
69 | |||
70 | ==== Compacted imports ==== | ||
71 | |||
72 | Two lines in Java are compacted into one in Scala. | ||
73 | |||
74 | In Java: | ||
75 | |||
76 | {{code}} | ||
77 | |||
78 | import com.webobjects.eocontrol.EOGenericRecord; | ||
79 | import com.webobjects.eocontrol.EORelationshipManipulation; | ||
80 | |||
81 | {{/code}} | ||
82 | |||
83 | In Scala: | ||
84 | |||
85 | {{code}} | ||
86 | |||
87 | import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation} | ||
88 | |||
89 | {{/code}} | ||
90 | |||
91 | == WOComponents in Scala == | ||
92 | |||
93 | ==== Compact Constructors ==== | ||
94 | |||
95 | Scala allows for simpler use of multi-valued constructors than Java. | ||
96 | |||
97 | In Java: | ||
98 | |||
99 | {{code}} | ||
100 | |||
101 | public class MenuHeader extends WOComponent { | ||
102 | |||
103 | public MenuHeader(WOContext aContext) { | ||
104 | super(aContext); | ||
105 | } | ||
106 | |||
107 | {{/code}} | ||
108 | |||
109 | In Scala: | ||
110 | |||
111 | {{code}} | ||
112 | |||
113 | class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) { | ||
114 | |||
115 | {{/code}} | ||
116 | |||
117 | ==== Simplified Exception Handling ==== | ||
118 | |||
119 | Scala doesn't force you to catch exceptions unlike in Java. | ||
120 | In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions. | ||
121 | |||
122 | In Java: | ||
123 | |||
124 | {{code}} | ||
125 | |||
126 | try { | ||
127 | EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session()); | ||
128 | epi.setNextPage(context().page()); | ||
129 | nextPage = (WOComponent) epi; | ||
130 | } catch (IllegalArgumentException e) { | ||
131 | ErrorPageInterface epf = D2W.factory().errorPage(session()); | ||
132 | epf.setMessage(e.toString()); | ||
133 | epf.setNextPage(context().page()); | ||
134 | nextPage = (WOComponent) epf; | ||
135 | } | ||
136 | |||
137 | {{/code}} | ||
138 | |||
139 | In Scala: | ||
140 | |||
141 | {{code}} | ||
142 | |||
143 | try { | ||
144 | var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session) | ||
145 | epi.setNextPage(context.page) | ||
146 | nextPage = epi.asInstanceOf[WOComponent] | ||
147 | } catch { | ||
148 | case e: IllegalArgumentException => { | ||
149 | var epf: ErrorPageInterface = D2W.factory.errorPage(session) | ||
150 | epf.setMessage(e.toString) | ||
151 | epf.setNextPage(context.page) | ||
152 | nextPage = epf.asInstanceOf[WOComponent] | ||
153 | } | ||
154 | } | ||
155 | |||
156 | {{/code}} | ||
157 | |||
158 | == How to Use Scala Collections with EOF == | ||
159 | |||
160 | One of the benefits of Scala is its very powerful, concurrency-ready collection classes - primarily ##List##, ##Map##, ##Seq## and ##Set##. | ||
161 | Employing these instead of ##NSArray## and ##NSDictionary## in WebObjects/EOF may be challenging. | ||
162 | |||
163 | But one may modify the EO templates to produce API such as: | ||
164 | |||
165 | {{code}} | ||
166 | |||
167 | def movies: NSArray[EOGenericRecord] = { | ||
168 | storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[EOGenericRecord]] | ||
169 | } | ||
170 | |||
171 | def moviesList: List[EOGenericRecord] = { | ||
172 | movies.objects.toList | ||
173 | } | ||
174 | |||
175 | {{/code}} | ||
176 | |||
177 | == How to Add Scala to a WO Project == | ||
178 | |||
179 | {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}} | ||
180 | |||
181 | {{note title="Note"}} | ||
182 | |||
183 | This is for Eclipse/WOLips IDE | ||
184 | |||
185 | {{/note}} | ||
186 | |||
187 | == WO Scala Example == | ||
188 | |||
189 | The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app: | ||
190 | All the EO logic and WO components are in Scala. | ||
191 | Only the Application class is Java. | ||
192 | |||
193 | It is based on the D2W Movies example. | ||
194 | |||
195 | {{attachments patterns=".*zip"}}{{/attachments}} | ||
196 | |||
197 | === Setup === | ||
198 | |||
199 | 1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]] | ||
200 | 1. Install and start the OpenBase OBMovies database. | ||
201 | 1. Right-click on Application.java and run as a WOApplication (as usual). | ||
202 | |||
203 | ==== EO Templates ==== | ||
204 | |||
205 | When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor: | ||
206 | |||
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 | 1. Change the File Names Extension to "scala" | ||
209 | 1. In Destination Paths set the Superclass Package (e.g: base) | ||
210 | 1. Uncheck Java under Options |