Wiki source code of WebObjects with Scala
Version 432.1 by Ravi Mendis on 2010/01/18 00:11
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
21.1 | 1 | === What is Scala? === |
![]() |
195.1 | 2 | |
![]() |
431.1 | 3 | Scala is a language for concurrent computing. |
4 | In this day and age of multi-core processors, concurrent computing can't be ignored. | ||
![]() |
195.1 | 5 | |
![]() |
431.1 | 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. | ||
![]() |
294.1 | 8 | |
![]() |
431.1 | 9 | Here's a quick summary: |
10 | |||
![]() |
338.1 | 11 | |= |= Objective-C |= Java |= Scala |
12 | |= Mutable/Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | Yes | ||
![]() |
349.1 | 13 | |= Closures | Blocks (//Extension//) | No | Anonymous Functions |
![]() |
338.1 | 14 | |= Static variables | Yes | Yes | No |
15 | |= Static methods/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 | ||
![]() |
294.1 | 18 | |
19 | Other notable features include: | ||
20 | |||
![]() |
338.1 | 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 | ||
![]() |
294.1 | 24 | |
![]() |
338.1 | 25 | A fuller description of Scala can be found [[here>>http://en.wikipedia.org/wiki/Scala_(programming_language)]]. |
26 | |||
![]() |
195.1 | 27 | === Why Use Scala? === |
28 | |||
![]() |
431.1 | 29 | Scala is inherently thread-safe. |
30 | It has concurrency that is effectively built-in to the language. | ||
![]() |
195.1 | 31 | |
![]() |
431.1 | 32 | So for WebObjects developers, Scala offers itself as a powerful, safe and easy-to-use solution for concurrent applications. (In other words, Scala Actors can be used for problems that would have normally required threads). |
![]() |
318.1 | 33 | |
![]() |
294.1 | 34 | === Can WebObjects be Programmed In Scala? === |
![]() |
195.1 | 35 | |
![]() |
288.1 | 36 | Yes. It is very simple. |
![]() |
318.1 | 37 | Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward. |
![]() |
195.1 | 38 | |
![]() |
294.1 | 39 | = WebObjects In Scala = |
![]() |
195.1 | 40 | |
![]() |
294.1 | 41 | The following highlights some of the differences between Java and Scala in WebObjects: |
![]() |
195.1 | 42 | |
![]() |
294.1 | 43 | == EOs in Scala == |
44 | |||
![]() |
318.1 | 45 | === Thread-Safe Shared Vars === |
![]() |
294.1 | 46 | |
![]() |
318.1 | 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. | ||
![]() |
294.1 | 49 | |
![]() |
359.1 | 50 | The following is an example of the use of a //Companion Object// for Talent in Scala instead of Talent static fields in Java. |
![]() |
294.1 | 51 | |
![]() |
355.1 | 52 | Java: |
53 | |||
![]() |
393.1 | 54 | {{code value="java"}} |
![]() |
308.1 | 55 | |
![]() |
369.1 | 56 | public class _Talent extends EOGenericRecord { |
![]() |
294.1 | 57 | public static final String ENTITY_NAME = "Talent"; |
58 | |||
59 | {{/code}} | ||
60 | |||
![]() |
355.1 | 61 | Scala: |
![]() |
294.1 | 62 | |
63 | {{code}} | ||
64 | |||
![]() |
369.1 | 65 | object Talent extends EOGenericRecord { |
![]() |
294.1 | 66 | val ENTITY_NAME = "Talent" |
67 | |||
68 | {{/code}} | ||
69 | |||
![]() |
318.1 | 70 | ==== Compacted imports ==== |
![]() |
308.1 | 71 | |
![]() |
318.1 | 72 | Two lines in Java are compacted into one in Scala. |
![]() |
308.1 | 73 | |
![]() |
294.1 | 74 | In Java: |
75 | |||
![]() |
393.1 | 76 | {{code value="java"}} |
![]() |
294.1 | 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 | |||
![]() |
393.1 | 99 | {{code value="java"}} |
![]() |
294.1 | 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 | |||
![]() |
312.1 | 113 | class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) { |
![]() |
294.1 | 114 | |
115 | {{/code}} | ||
116 | |||
117 | ==== Simplified Exception Handling ==== | ||
118 | |||
119 | Scala doesn't force you to catch exceptions unlike in Java. | ||
![]() |
431.1 | 120 | In addition, the syntax employs Scala's very powerful **pattern matching** to handle exceptions. |
![]() |
294.1 | 121 | |
122 | In Java: | ||
123 | |||
![]() |
393.1 | 124 | {{code value="java"}} |
![]() |
294.1 | 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 | |||
![]() |
381.1 | 158 | ==== Scala Annotations vs. Generic Accessors ==== |
159 | |||
![]() |
385.1 | 160 | An example of accessing variables in WebObjects with the following languages: |
![]() |
381.1 | 161 | |
![]() |
385.1 | 162 | |= |= Objective-C |= Java |= Scala |
163 | |= getter | ##object name## | ##object.name()## | ##object.name## | ||
164 | |= setter | ##object setName:aName## | ##object.setName(aName)## | ##object.name = aName## | ||
![]() |
381.1 | 165 | |
![]() |
385.1 | 166 | Of course in Java, we may generate WebObjects classes with "get" methods as well in order to stick to convention. |
167 | 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. | ||
168 | |||
169 | E.g, in Main.scala we annotate our component keys with ##@BeanProperty## to automatically create public "set" and "get" methods. | ||
170 | These variables can then be accessed via //KVC//. | ||
171 | |||
172 | {{code}} | ||
173 | |||
174 | @BeanProperty var username = new String() | ||
175 | @BeanProperty var password = new String() | ||
176 | @BeanProperty var isAssistantCheckboxVisible = false | ||
177 | |||
178 | {{/code}} | ||
179 | |||
![]() |
353.1 | 180 | == How to Use Scala Collections with EOF == |
![]() |
351.1 | 181 | |
182 | One of the benefits of Scala is its very powerful, concurrency-ready collection classes - primarily ##List##, ##Map##, ##Seq## and ##Set##. | ||
183 | Employing these instead of ##NSArray## and ##NSDictionary## in WebObjects/EOF may be challenging. | ||
184 | |||
185 | But one may modify the EO templates to produce API such as: | ||
186 | |||
187 | {{code}} | ||
188 | |||
189 | def movies: NSArray[EOGenericRecord] = { | ||
190 | storedValueForKey(_Studio.Keys.MOVIES).asInstanceOf[NSArray[EOGenericRecord]] | ||
191 | } | ||
192 | |||
193 | def moviesList: List[EOGenericRecord] = { | ||
194 | movies.objects.toList | ||
195 | } | ||
196 | |||
197 | {{/code}} | ||
198 | |||
![]() |
318.1 | 199 | == How to Add Scala to a WO Project == |
![]() |
308.1 | 200 | |
201 | {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}} | ||
202 | |||
![]() |
338.1 | 203 | {{note title="Note"}} |
![]() |
318.1 | 204 | |
205 | This is for Eclipse/WOLips IDE | ||
206 | |||
207 | {{/note}} | ||
208 | |||
![]() |
290.1 | 209 | == WO Scala Example == |
210 | |||
![]() |
353.1 | 211 | The following example is an almost 100% Scala WO app. In reality it is a mixed Java/Scala app: |
![]() |
292.1 | 212 | All the EO logic and WO components are in Scala. |
![]() |
318.1 | 213 | Only the Application class is Java. |
![]() |
292.1 | 214 | |
![]() |
353.1 | 215 | It is based on the D2W Movies example. |
216 | |||
![]() |
290.1 | 217 | {{attachments patterns=".*zip"}}{{/attachments}} |
![]() |
294.1 | 218 | |
219 | === Setup === | ||
220 | |||
![]() |
318.1 | 221 | 1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]] |
![]() |
353.1 | 222 | 1. Install and start the OpenBase OBMovies database. |
![]() |
294.1 | 223 | 1. Right-click on Application.java and run as a WOApplication (as usual). |
224 | |||
![]() |
318.1 | 225 | ==== EO Templates ==== |
![]() |
294.1 | 226 | |
227 | When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor: | ||
228 | |||
![]() |
357.1 | 229 | 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//## |
![]() |
294.1 | 230 | 1. Change the File Names Extension to "scala" |
![]() |
367.1 | 231 | 1. In Destination Paths set the Superclass Package (e.g: base) |
![]() |
294.1 | 232 | 1. Uncheck Java under Options |
![]() |
385.1 | 233 | |
234 | == How to Build & Deploy a WebObjects Scala Project with Ant == | ||
235 | |||
236 | 1. [[Download>>http://www.scala-lang.org/downloads]] and install Scala | ||
![]() |
393.1 | 237 | 1. Set ##scala.home## (the location Scala has been installed onto) in the project ##build.properties## file |
238 | 1. [[Add the scalac task and properties>>Configuring Ant to Build Scala with WebObjects]] to the ant build.xml file | ||
239 | 1. Run from the project directory: ##sudo ant clean install## |