Wiki source code of WebObjects with Scala
Version 223.1 by Ravi Mendis on 2009/09/16 18:46
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 Rails which has been the reason for its adoption at sites like Twitter. | ||
| 5 | |||
| 6 | Many of its features and paradigms favor multi-threaded and concurrent development which 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 | |||
| 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 | So it may offer new solutions for concurrency in WebObjects. | ||
| 31 | |||
| 32 | === Can WebObjects be Programmed In Scala? === | ||
| 33 | |||
| 34 | Yes. It is very simple. | ||
| 35 | Scala compiles to java bytecode. Hence using it with WebObjects is fairly straightforward. | ||
| 36 | |||
| 37 | = WebObjects In Scala = | ||
| 38 | |||
| 39 | The following is an in-depth explanation of differences between Java and Scala in WebObjects: | ||
| 40 | |||
| 41 | == EOs in Scala == | ||
| 42 | |||
| 43 | === Constants === | ||
| 44 | |||
| 45 | Scala doesn't have static variables or methods. However, every class does 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 | What is five lines in Java: | ||
| 69 | |||
| 70 | {{code}} | ||
| 71 | |||
| 72 | import com.webobjects.eocontrol.EOGenericRecord; | ||
| 73 | import com.webobjects.eocontrol.EORelationshipManipulation; | ||
| 74 | import com.webobjects.foundation.NSArray; | ||
| 75 | import com.webobjects.foundation.NSData; | ||
| 76 | import org.apache.log4j.Logger; | ||
| 77 | |||
| 78 | {{/code}} | ||
| 79 | |||
| 80 | Is three lines in Scala: | ||
| 81 | |||
| 82 | {{code}} | ||
| 83 | |||
| 84 | import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation} | ||
| 85 | import com.webobjects.foundation.{NSArray, NSData} | ||
| 86 | import org.apache.log4j.Logger | ||
| 87 | |||
| 88 | {{/code}} | ||
| 89 | |||
| 90 | == How to Program WebObjects In Scala == | ||
| 91 | |||
| 92 | {{include value="WOL:Adding Scala Support to a WOLips Project"}}{{/include}} | ||
| 93 | |||
| 94 | {{note title="Note"}} | ||
| 95 | |||
| 96 | This is for Eclipse/WOLips IDE | ||
| 97 | |||
| 98 | {{/note}} | ||
| 99 | |||
| 100 | == WO Scala Example == | ||
| 101 | |||
| 102 | The following is a mixed Java/Scala version of the WO Movies D2W app. | ||
| 103 | All the EO logic and WO components are in Scala. | ||
| 104 | Only the Application class is Java. | ||
| 105 | |||
| 106 | {{attachments patterns=".*zip"}}{{/attachments}} | ||
| 107 | |||
| 108 | === Setup === | ||
| 109 | |||
| 110 | 1. Install and run the OpenBase OBMovies database. | ||
| 111 | 1. Right-click on Application.java and run as a WOApplication (as usual). | ||
| 112 | |||
| 113 | ==== EO Templates ==== | ||
| 114 | |||
| 115 | See: [[Scala templates>>http://wiki.objectstyle.org/confluence/display/WOL/EOGenerator+Templates+and+Additions]] | ||
| 116 | |||
| 117 | When you create your ##.eogen## file, be sure to make the following changes in the EOGenerator Editor: | ||
| 118 | |||
| 119 | 1. Change the File Names Extension to "scala" | ||
| 120 | 1. Uncheck Java under Options |