Wiki source code of WebObjects with Scala

Version 306.1 by Ravi Mendis on 2009/09/17 21:46

Hide last authors
Ravi Mendis 21.1 1 === What is Scala? ===
Ravi Mendis 195.1 2
3 Scala is a modern language not unlike Groovy.
Ravi Mendis 294.1 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.
Ravi Mendis 195.1 5
Ravi Mendis 302.1 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:
Ravi Mendis 294.1 7
8 |= |= Objective-C |= Java |= Scala
Ravi Mendis 304.1 9 |= Mutable/Immuable Datatypes | Collections //e.g: NSArray/NSMutableArray// | No | //All datatypes//
Ravi Mendis 298.1 10 |= Closures | Blocks (//Extension//) | No | Yes
Ravi Mendis 294.1 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
Ravi Mendis 195.1 24 === Why Use Scala? ===
25
Ravi Mendis 294.1 26 With Web 2.0, building concurrent WebObjects applications is a must.
Ravi Mendis 195.1 27 Developing and maintaining a concurrent or multi-threaded WebObjects application can be challenging.
28
Ravi Mendis 294.1 29 Scala offers concurrency that is (effectively) built-in to the language and is inherently thread-safe.
Ravi Mendis 300.1 30 Developing Ajax (i.e asynchronous communication) with WO may well require concurrent request handling and thread-safe code, for which Scala is a better choice than Java.
Ravi Mendis 195.1 31
Ravi Mendis 300.1 32 In addition it may offer new solutions for concurrency in WebObjects.
33
Ravi Mendis 294.1 34 === Can WebObjects be Programmed In Scala? ===
Ravi Mendis 195.1 35
Ravi Mendis 288.1 36 Yes. It is very simple.
37 Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward.
Ravi Mendis 195.1 38
Ravi Mendis 294.1 39 = WebObjects In Scala =
Ravi Mendis 195.1 40
Ravi Mendis 294.1 41 The following highlights some of the differences between Java and Scala in WebObjects:
Ravi Mendis 195.1 42
Ravi Mendis 294.1 43 == EOs in Scala ==
44
Ravi Mendis 306.1 45 === Thread-Safe Shared Vars ===
Ravi Mendis 294.1 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 In Java:
51
52 {{code}}
53
54 public class _Talent extends EOGenericRecord {
55 public static final String ENTITY_NAME = "Talent";
56
57 {{/code}}
58
59 In Scala:
60
61 {{code}}
62
63 object _Talent extends EOGenericRecord {
64 val ENTITY_NAME = "Talent"
65
66 {{/code}}
67
68 ==== Compacted imports ====
69
70 Two lines in Java is compacted into one in Scala.
71
72 In Java:
73
74 {{code}}
75
76 import com.webobjects.eocontrol.EOGenericRecord;
77 import com.webobjects.eocontrol.EORelationshipManipulation;
78
79 {{/code}}
80
81 In Scala:
82
83 {{code}}
84
85 import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation}
86
87 {{/code}}
88
89 == WOComponents in Scala ==
90
91 ==== Compact Constructors ====
92
93 Scala allows for simpler use of multi-valued constructors than Java.
94
95 In Java:
96
97 {{code}}
98
99 public class MenuHeader extends WOComponent {
100
101 public MenuHeader(WOContext aContext) {
102 super(aContext);
103 }
104
105 {{/code}}
106
107 In Scala:
108
109 {{code}}
110
111 class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) {
112
113 {{/code}}
114
115 ==== Simplified Exception Handling ====
116
117 Scala doesn't force you to catch exceptions unlike in Java.
118 In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions.
119
120 In Java:
121
122 {{code}}
123
124 try {
125 EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session());
126 epi.setNextPage(context().page());
127 nextPage = (WOComponent) epi;
128 } catch (IllegalArgumentException e) {
129 ErrorPageInterface epf = D2W.factory().errorPage(session());
130 epf.setMessage(e.toString());
131 epf.setNextPage(context().page());
132 nextPage = (WOComponent) epf;
133 }
134
135 {{/code}}
136
137 In Scala:
138
139 {{code}}
140
141 try {
142 var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session)
143 epi.setNextPage(context.page)
144 nextPage = epi.asInstanceOf[WOComponent]
145 } catch {
146 case e: IllegalArgumentException => {
147 var epf: ErrorPageInterface = D2W.factory.errorPage(session)
148 epf.setMessage(e.toString)
149 epf.setNextPage(context.page)
150 nextPage = epf.asInstanceOf[WOComponent]
151 }
152 }
153
154 {{/code}}
155
156 == How to Add Scala to a WO Project ==
157
158 {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}}
159
Ravi Mendis 195.1 160 {{note title="Note"}}
161
Ravi Mendis 294.1 162 This is for Eclipse/WOLips IDE
Ravi Mendis 195.1 163
164 {{/note}}
Ravi Mendis 290.1 165
166 == WO Scala Example ==
167
Ravi Mendis 294.1 168 The following example is a mixed Java/Scala version of the WO Movies D2W app.
Ravi Mendis 292.1 169 All the EO logic and WO components are in Scala.
170 Only the Application class is Java.
171
Ravi Mendis 290.1 172 {{attachments patterns=".*zip"}}{{/attachments}}
Ravi Mendis 294.1 173
174 === Setup ===
175
Ravi Mendis 298.1 176 1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]]
Ravi Mendis 294.1 177 1. Install and run the OpenBase OBMovies database.
178 1. Right-click on Application.java and run as a WOApplication (as usual).
179
180 ==== EO Templates ====
181
182 See: [[Scala templates>>http://wiki.objectstyle.org/confluence/display/WOL/EOGenerator+Templates+and+Additions]]
183
184 When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor:
185
186 1. Change the File Names Extension to "scala"
187 1. Uncheck Java under Options