Wiki source code of WebObjects with Scala
Version 478.1 by Ravi Mendis on 2010/12/23 23:53
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | === What is Scala? === | ||
2 | |||
3 | [[Scala>>http://en.wikipedia.org/wiki/Scala_(programming_language)]] is a JVM language that is a hybrid of Object-Oriented and Functional styles. Its [[functional language>>http://en.wikipedia.org/wiki/Functional_programming]] foundations and built-in Actors library makes it especially attractive for concurrent computing. In this day and age of multi-core processors concurrent computing can not be ignored. | ||
4 | |||
5 | Many of Scala's features have been designed with concurrency in mind, some of which may not be unfamiliar to Objective-C or WebObjects developers. Here's a summary: | ||
6 | |||
7 | |= |= Objective-C |= Java |= Scala | ||
8 | |= Immutability | Collections //e.g: NSArray/NSMutableArray// | No | Yes | ||
9 | |= Closures | Blocks (//Extension//) | No | Anonymous Functions | ||
10 | |= Static variables | Yes | Yes | No | ||
11 | |= Static methods or functions | Yes | Yes | No | ||
12 | |= Concurrency | [[Grand Central Dispatch>>http://en.wikipedia.org/wiki/Grand_Central_Dispatch]] (//Extension//) | //Threads// | [[Actors>>http://en.wikipedia.org/wiki/Actor_model]] | ||
13 | |= |= Weakly Typed |= --Strongly Typed-- |= Strongly Typed | ||
14 | |||
15 | Other notable features include: | ||
16 | |||
17 | |= |= Objective-C |= Java |= Scala | ||
18 | |= Parametered methods | Yes //e.g: addObject: to~:// | No | Yes //e.g: add(object= ,to=)// | ||
19 | |= Class composition | Categories | Interfaces | Traits | ||
20 | |||
21 | === Why Use Scala? === | ||
22 | |||
23 | Scala can help you to write thread-safe code. | ||
24 | It has concurrency that is built-in to the standard library, primarily via Actors. | ||
25 | |||
26 | 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). | ||
27 | |||
28 | === Can WebObjects be Programmed In Scala? === | ||
29 | |||
30 | Yes. It is very simple. | ||
31 | Scala compiles to java bytecode. | ||
32 | |||
33 | Furthermore, being a multi-paradigm language grants Scala easy WebObjects-interoperability. | ||
34 | |||
35 | ===== Caveats ===== | ||
36 | |||
37 | Tool support is a weak point. If your Application class is in Scala then you will have to create an Eclipse launch configuration manually. Also the [[Scala plugin>>http://www.scala-ide.org]] is rather slow and still very buggy. | ||
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. 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. This is not however true for mutable ##val## e.g: ##NSMutableArray##, ##scala.collection.mutable.ListBuffer## | ||
48 | |||
49 | So you don't have to worry about synchronizing access to shared mutable fields in a concurrent application. | ||
50 | |||
51 | The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java. | ||
52 | |||
53 | Java: | ||
54 | |||
55 | {{code value="java"}} | ||
56 | |||
57 | public class _Talent extends EOGenericRecord { | ||
58 | public static final String ENTITY_NAME = "Talent"; | ||
59 | |||
60 | {{/code}} | ||
61 | |||
62 | Scala: | ||
63 | |||
64 | {{code}} | ||
65 | |||
66 | object Talent extends EOGenericRecord { | ||
67 | val ENTITY_NAME = "Talent" | ||
68 | |||
69 | {{/code}} | ||
70 | |||
71 | This value will be accessed exactly the same way in both languages: | ||
72 | |||
73 | {{code}} | ||
74 | |||
75 | Talent.ENTITY_NAME | ||
76 | |||
77 | {{/code}} | ||
78 | |||
79 | ==== Compacted imports ==== | ||
80 | |||
81 | Two lines in Java are compacted into one in Scala. | ||
82 | |||
83 | In Java: | ||
84 | |||
85 | {{code value="java"}} | ||
86 | |||
87 | import com.webobjects.eocontrol.EOGenericRecord; | ||
88 | import com.webobjects.eocontrol.EORelationshipManipulation; | ||
89 | |||
90 | {{/code}} | ||
91 | |||
92 | In Scala: | ||
93 | |||
94 | {{code}} | ||
95 | |||
96 | import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation} | ||
97 | |||
98 | {{/code}} | ||
99 | |||
100 | == WOComponents in Scala == | ||
101 | |||
102 | ==== Compact Constructors ==== | ||
103 | |||
104 | Scala allows for simpler use of multi-valued constructors than Java. | ||
105 | |||
106 | In Java: | ||
107 | |||
108 | {{code value="java"}} | ||
109 | |||
110 | public class MenuHeader extends WOComponent { | ||
111 | |||
112 | public MenuHeader(WOContext aContext) { | ||
113 | super(aContext); | ||
114 | } | ||
115 | |||
116 | {{/code}} | ||
117 | |||
118 | In Scala: | ||
119 | |||
120 | {{code}} | ||
121 | |||
122 | class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) { | ||
123 | |||
124 | {{/code}} | ||
125 | |||
126 | ==== Simplified Exception Handling ==== | ||
127 | |||
128 | Scala doesn't force you to catch exceptions unlike in Java. | ||
129 | In addition, the syntax employs Scala's very powerful **pattern matching** to handle exceptions. | ||
130 | |||
131 | In Java: | ||
132 | |||
133 | {{code value="java"}} | ||
134 | |||
135 | try { | ||
136 | EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session()); | ||
137 | epi.setNextPage(context().page()); | ||
138 | nextPage = (WOComponent) epi; | ||
139 | } catch (IllegalArgumentException e) { | ||
140 | ErrorPageInterface epf = D2W.factory().errorPage(session()); | ||
141 | epf.setMessage(e.toString()); | ||
142 | epf.setNextPage(context().page()); | ||
143 | nextPage = (WOComponent) epf; | ||
144 | } | ||
145 | |||
146 | {{/code}} | ||
147 | |||
148 | In Scala: | ||
149 | |||
150 | {{code}} | ||
151 | |||
152 | try { | ||
153 | var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session) | ||
154 | epi.setNextPage(context.page) | ||
155 | nextPage = epi.asInstanceOf[WOComponent] | ||
156 | } catch { | ||
157 | case e: IllegalArgumentException => { | ||
158 | var epf: ErrorPageInterface = D2W.factory.errorPage(session) | ||
159 | epf.setMessage(e.toString) | ||
160 | epf.setNextPage(context.page) | ||
161 | nextPage = epf.asInstanceOf[WOComponent] | ||
162 | } | ||
163 | } | ||
164 | |||
165 | {{/code}} | ||
166 | |||
167 | ==== Scala Annotations vs. Generated Accessors ==== | ||
168 | |||
169 | An example of accessing variables in WebObjects with the following languages: | ||
170 | |||
171 | |= |= Objective-C |= Java |= Scala | ||
172 | |= getter | ##object name## | ##object.name()## | ##object.name## | ||
173 | |= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName## | ||
174 | |||
175 | Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention. | ||
176 | 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. | ||
177 | |||
178 | E.g, in Main.scala we annotate our component keys with ##@BeanProperty## to automatically create public "set" and "get" methods. | ||
179 | These variables can then be accessed via //KVC//. | ||
180 | |||
181 | {{code}} | ||
182 | |||
183 | import scala.reflect.BeanProperty | ||
184 | |||
185 | @BeanProperty var username = new String() | ||
186 | @BeanProperty var password = new String() | ||
187 | @BeanProperty var isAssistantCheckboxVisible = false | ||
188 | |||
189 | {{/code}} | ||
190 | |||
191 | == How to Use Scala Collections with EOF == | ||
192 | |||
193 | To use the Scala Collections API with an NSArray or NSDictionary you simply need to add an import: | ||
194 | |||
195 | {{code value="java"}} | ||
196 | |||
197 | import scala.collection.JavaConversions._ | ||
198 | |||
199 | {{/code}} | ||
200 | |||
201 | 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. Alternatively, you could generate an actual new scala.List instance by calling myNSArray.toList. | ||
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}} | ||
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 remains 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-ide.org/]] | ||
226 | 1. Right-click on Application.java and run as a WOApplication (as usual). | ||
227 | |||
228 | Application can be made into a Scala class as well, but then you will have to create a launcher in Eclipse manually. | ||
229 | |||
230 | == EO Templates == | ||
231 | |||
232 | When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor: | ||
233 | |||
234 | 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## | ||
235 | 1. Change the File Names Extension to "scala" | ||
236 | 1. In Destination Paths set the Superclass Package (e.g: base) | ||
237 | 1. Uncheck Java under Options | ||
238 | |||
239 | == How to Build & Deploy a WebObjects Scala Project with Ant == | ||
240 | |||
241 | 1. [[Download>>http://www.scala-lang.org/downloads]] and install Scala | ||
242 | 1. Set ##scala.home## (the location Scala has been installed onto) in the project ##build.properties## file | ||
243 | 1. [[Add the scalac task and properties>>Configuring Ant to Build Scala with WebObjects]] to the ant build.xml file | ||
244 | 1. Run from the project directory: ##sudo ant clean install## |