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