Changes for page WebObjects with Scala

Last modified by Ravi Mendis on 2011/05/10 02:10

From version 293.1
edited by Ravi Mendis
on 2009/09/15 19:36
Change comment: There is no comment for this version
To version 294.1
edited by Ravi Mendis
on 2009/09/17 00:58
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,39 +1,185 @@
1 1  === What is Scala? ===
2 2  
3 3  Scala is a modern language not unlike Groovy.
4 -It is said to be more powerful (and faster) than Groovy or Rails which has been the reason for its adoption at sites like Twitter.
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 5  
6 +Many of its features and paradigms favor multi-threading and concurrency. 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 | //All datatypes//
10 +|= Closures | Blocks (//Extension//) | No | //Built-in//
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 +
6 6  === Why Use Scala? ===
7 7  
8 -In a Web 2.0, building concurrent WebObjects applications is a must.
26 +With Web 2.0, building concurrent WebObjects applications is a must.
9 9  Developing and maintaining a concurrent or multi-threaded WebObjects application can be challenging.
10 10  
11 -Scala offers concurrency built into the language.
12 -So it may offer new solutions to WO developers...
29 +Scala offers concurrency that is (effectively) built-in to the language and is inherently thread-safe.
30 +So it may offer new solutions for concurrency in WebObjects.
13 13  
14 -=== Can WebObjects be Programmed With Scala? ===
32 +=== Can WebObjects be Programmed In Scala? ===
15 15  
16 16  Yes. It is very simple.
17 17  Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward.
18 18  
19 -== How to Develop WebObjects With Scala ==
37 += WebObjects In Scala =
20 20  
21 -1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]]
22 -1. Add Scala support to your project
23 -11. Right-click your project in the WO Explorer
24 -11. In the context menu select Scala -> Add Scala Nature
25 -1. Convert or use ##.scala## instead of ##.java## source
39 +The following highlights some of the differences between Java and Scala in WebObjects:
26 26  
41 +== EOs in Scala ==
42 +
43 +=== Thread-Safe Shared Constants and Vars ===
44 +
45 +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.
46 +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.
47 +
48 +In Java:
49 +
50 +{{code}}
51 +
52 +public class _Talent extends EOGenericRecord {
53 + public static final String ENTITY_NAME = "Talent";
54 +
55 +{{/code}}
56 +
57 +In Scala:
58 +
59 +{{code}}
60 +
61 +object _Talent extends EOGenericRecord {
62 + val ENTITY_NAME = "Talent"
63 +
64 +{{/code}}
65 +
66 +==== Compacted imports ====
67 +
68 +Two lines in Java is compacted into one in Scala.
69 +
70 +In Java:
71 +
72 +{{code}}
73 +
74 +import com.webobjects.eocontrol.EOGenericRecord;
75 +import com.webobjects.eocontrol.EORelationshipManipulation;
76 +
77 +{{/code}}
78 +
79 +In Scala:
80 +
81 +{{code}}
82 +
83 +import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation}
84 +
85 +{{/code}}
86 +
87 +== WOComponents in Scala ==
88 +
89 +==== Compact Constructors ====
90 +
91 +Scala allows for simpler use of multi-valued constructors than Java.
92 +
93 +In Java:
94 +
95 +{{code}}
96 +
97 +public class MenuHeader extends WOComponent {
98 +
99 + public MenuHeader(WOContext aContext) {
100 + super(aContext);
101 + }
102 +
103 +{{/code}}
104 +
105 +In Scala:
106 +
107 +{{code}}
108 +
109 +class MenuHeader(context: WOContext) extends WOComponent(context: WOContext) {
110 +
111 +{{/code}}
112 +
113 +==== Simplified Exception Handling ====
114 +
115 +Scala doesn't force you to catch exceptions unlike in Java.
116 +In addition, the syntax employs Scala's very powerful pattern matching to handle different exceptions.
117 +
118 +In Java:
119 +
120 +{{code}}
121 +
122 +try {
123 + EditPageInterface epi = D2W.factory().editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session());
124 + epi.setNextPage(context().page());
125 + nextPage = (WOComponent) epi;
126 +} catch (IllegalArgumentException e) {
127 + ErrorPageInterface epf = D2W.factory().errorPage(session());
128 + epf.setMessage(e.toString());
129 + epf.setNextPage(context().page());
130 + nextPage = (WOComponent) epf;
131 +}
132 +
133 +{{/code}}
134 +
135 +In Scala:
136 +
137 +{{code}}
138 +
139 +try {
140 + var epi: EditPageInterface = D2W.factory.editPageForNewObjectWithEntityNamed(_manipulatedEntityName, session)
141 + epi.setNextPage(context.page)
142 + nextPage = epi.asInstanceOf[WOComponent]
143 +} catch {
144 + case e: IllegalArgumentException => {
145 + var epf: ErrorPageInterface = D2W.factory.errorPage(session)
146 + epf.setMessage(e.toString)
147 + epf.setNextPage(context.page)
148 + nextPage = epf.asInstanceOf[WOComponent]
149 + }
150 +}
151 +
152 +{{/code}}
153 +
154 +== How to Add Scala to a WO Project ==
155 +
156 +{{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}}
157 +
27 27  {{note title="Note"}}
28 28  
29 -This is for Eclipse/WOLips developers
160 +This is for Eclipse/WOLips IDE
30 30  
31 31  {{/note}}
32 32  
33 33  == WO Scala Example ==
34 34  
35 -The following is a mixed Java/Scala version of the WO Movies D2W app.
166 +The following example is a mixed Java/Scala version of the WO Movies D2W app.
36 36  All the EO logic and WO components are in Scala.
37 37  Only the Application class is Java.
38 38  
39 39  {{attachments patterns=".*zip"}}{{/attachments}}
171 +
172 +=== Setup ===
173 +
174 +1. [[Install the Scala eclipse IDE>>http://www.scala-lang.org/node/94]]
175 +1. Install and run the OpenBase OBMovies database.
176 +1. Right-click on Application.java and run as a WOApplication (as usual).
177 +
178 +==== EO Templates ====
179 +
180 +See: [[Scala templates>>http://wiki.objectstyle.org/confluence/display/WOL/EOGenerator+Templates+and+Additions]]
181 +
182 +When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor:
183 +
184 +1. Change the File Names Extension to "scala"
185 +1. Uncheck Java under Options