Wiki source code of WebObjects with Scala

Version 410.1 by Ravi Mendis on 2010/01/14 23:04

Show last authors
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.
5 This's been the reason for its adoption at sites like Twitter.
6
7 Many of its features and paradigms favor multi-threading and concurrency.
8 It could be said that Scala was designed from the ground up with concurrency in mind.
9
10 Some of its features may not be unfamiliar to Objective-C or WebObjects developers.
11 Here's a quick summary:
12
13 |= |= Objective-C |= Java |= Scala
14 |= Mutable/Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | Yes
15 |= Closures | Blocks (//Extension//) | No | Anonymous Functions
16 |= Static variables | Yes | Yes | No
17 |= Static methods/functions | Yes | Yes | No
18 |= Concurrency | [[Grand Central Dispatch>>http://en.wikipedia.org/wiki/Grand_Central_Dispatch]] (//Extension//)| //Threads// |[[Actors>>http://en.wikipedia.org/wiki/Actor_model]]
19 |= |= Weakly Typed |=--Strongly Typed--|= Strongly Typed
20
21 Other notable features include:
22
23 |= |= Objective-C |= Java |= Scala
24 |= Parametered methods | Yes //e.g: addObject: to~:// | No | Yes //e.g: add(object= ,to=)//
25 |= Class composition | Categories | Interfaces | Traits
26
27 A fuller description of Scala can be found [[here>>http://en.wikipedia.org/wiki/Scala_(programming_language)]].
28
29 === Why Use Scala? ===
30
31 Developing and maintaining a concurrent or multi-threaded WebObjects application can be challenging.
32
33 The lack of static variables means that Scala is inherently thread-safe.
34 It has concurrency that is effectively built-in to the language in the form of Actors.
35
36 So for WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for concurrent applications.
37
38 === Can WebObjects be Programmed In Scala? ===
39
40 Yes. It is very simple.
41 Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward.
42
43 = WebObjects In Scala =
44
45 The following highlights some of the differences between Java and Scala in WebObjects:
46
47 == EOs in Scala ==
48
49 === Thread-Safe Shared Vars ===
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.
53
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
56 Java:
57
58 {{code value="java"}}
59
60 public class _Talent extends EOGenericRecord {
61 public static final String ENTITY_NAME = "Talent";
62
63 {{/code}}
64
65 Scala:
66
67 {{code}}
68
69 object Talent extends EOGenericRecord {
70 val ENTITY_NAME = "Talent"
71
72 {{/code}}
73
74 ==== Compacted imports ====
75
76 Two lines in Java are compacted into one in Scala.
77
78 In Java:
79
80 {{code value="java"}}
81
82 import com.webobjects.eocontrol.EOGenericRecord;
83 import com.webobjects.eocontrol.EORelationshipManipulation;
84
85 {{/code}}
86
87 In Scala:
88
89 {{code}}
90
91 import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation}
92
93 {{/code}}
94
95 == WOComponents in Scala ==
96
97 ==== Compact Constructors ====
98
99 Scala allows for simpler use of multi-valued constructors than Java.
100
101 In Java:
102
103 {{code value="java"}}
104
105 public class MenuHeader extends WOComponent {
106
107 public MenuHeader(WOContext aContext) {
108 super(aContext);
109 }
110
111 {{/code}}
112
113 In Scala:
114
115 {{code}}
116
117 class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) {
118
119 {{/code}}
120
121 ==== Simplified Exception Handling ====
122
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.
125
126 In Java:
127
128 {{code value="java"}}
129
130 try {
131 EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session());
132 epi.setNextPage(context().page());
133 nextPage = (WOComponent) epi;
134 } catch (IllegalArgumentException e) {
135 ErrorPageInterface epf = D2W.factory().errorPage(session());
136 epf.setMessage(e.toString());
137 epf.setNextPage(context().page());
138 nextPage = (WOComponent) epf;
139 }
140
141 {{/code}}
142
143 In Scala:
144
145 {{code}}
146
147 try {
148 var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session)
149 epi.setNextPage(context.page)
150 nextPage = epi.asInstanceOf[WOComponent]
151 } catch {
152 case e: IllegalArgumentException => {
153 var epf: ErrorPageInterface = D2W.factory.errorPage(session)
154 epf.setMessage(e.toString)
155 epf.setNextPage(context.page)
156 nextPage = epf.asInstanceOf[WOComponent]
157 }
158 }
159
160 {{/code}}
161
162 ==== Scala Annotations vs. Generic Accessors ====
163
164 An example of accessing variables in WebObjects with the following languages:
165
166 |= |= Objective-C |= Java |= Scala
167 |= getter | ##object name## | ##object.name()## | ##object.name##
168 |= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName##
169
170 Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention.
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.
172
173 E.g, in Main.scala we annotate our component keys with ##@BeanProperty## to automatically create public "set" and "get" methods.
174 These variables can then be accessed via //KVC//.
175
176 {{code}}
177
178 @BeanProperty var username = new String()
179 @BeanProperty var password = new String()
180 @BeanProperty var isAssistantCheckboxVisible = false
181
182 {{/code}}
183
184 == How to Use Scala Collections with EOF ==
185
186 One of the benefits of Scala is its very powerful, concurrency-ready collection classes - primarily ##List##, ##Map##, ##Seq## and ##Set##.
187 Employing these instead of ##NSArray## and ##NSDictionary## in WebObjects/EOF may be challenging.
188
189 But one may modify the EO templates to produce API such as:
190
191 {{code}}
192
193 def movies: NSArray[EOGenericRecord] = {
194 storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[EOGenericRecord]]
195 }
196
197 def moviesList: List[EOGenericRecord] = {
198 movies.objects.toList
199 }
200
201 {{/code}}
202
203 == How to Add Scala to a WO Project ==
204
205 {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}}
206
207 {{note title="Note"}}
208
209 This is for Eclipse/WOLips IDE
210
211 {{/note}}
212
213 == WO Scala Example ==
214
215 The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app:
216 All the EO logic and WO components are in Scala.
217 Only the Application class is Java.
218
219 It is based on the D2W Movies example.
220
221 {{attachments patterns=".*zip"}}{{/attachments}}
222
223 === Setup ===
224
225 1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]]
226 1. Install and start the OpenBase OBMovies database.
227 1. Right-click on Application.java and run as a WOApplication (as usual).
228
229 ==== EO Templates ====
230
231 When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor:
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//##
234 1. Change the File Names Extension to "scala"
235 1. In Destination Paths set the Superclass Package (e.g: base)
236 1. Uncheck Java under Options
237
238 == How to Build & Deploy a WebObjects Scala Project with Ant ==
239
240 1. [[Download>>http://www.scala-lang.org/downloads]] and install Scala
241 1. Set ##scala.home## (the location Scala has been installed onto) in the project ##build.properties## file
242 1. [[Add the scalac task and properties>>Configuring Ant to Build Scala with WebObjects]] to the ant build.xml file
243 1. Run from the project directory: ##sudo ant clean install##