I've been excited about
Scala recently, for a number of reasons -- it's a modern language with lots of great features that you might expect if you've been looking at Haskell or ML. It's got pattern matching, a concise syntax, type inference, and first-order functions. The really killer thing about Scala, though, is how well it integrates with Java.
You can very easily have a mixed Scala and Java project, and make calls back and forth; Scala and Java packages sit in the same package hierarchy and compile to the same bytecode -- it all just works,
pretty much seamlessly.
So, clearly, the right thing to do is hook Scala up to
GWT RPC and run it on
App Engine. Let's do that. This is as awesome as it's going to get, at least until the GWT compiler supports Scala and we can do our client side in Scala too.
I assume you've already installed both the
Google Pluin for Eclipse and the
Scala IDE for Eclipse. I'm using Eclipse 3.4.
Make a new "Web Application" project (with
File > New, or just click the blue "g" icon). Click the boxes for both "Google Web Toolkit" and "App Engine" -- our client side will be in GWT, and the server on App Engine.
Right now, our new project has "Java" nature, but not the "Scala" nature. Add Scala by right-clicking the project and choosing
Scala > Add Scala Nature. We're also going to make sure we have a copy of the Scala runtime library once we deploy to the server. Find out where
scala-library.jar
sits (expand out the "Scala Library" container in your project) and copy it into the project's
war/WEB-INF/lib
.
Now, down to business. There's a RemoteServiceServlet currently implemented in Java, and we're going to replace it with Scala code -- this is the thing that gets called on the server side when the client makes an RPC request. Find
GreetingServiceImpl.java
and delete it.
To replace it with a class implemented in Scala, right-click your "gwtscalademo.server" package and select
New > Other > Scala Wizards > Scala Class. The class we deleted was a servlet and referenced by the web.xml, so give the Scala class the same name: GreetingServiceImpl.
Here's my version:
package gwtscalademo.server
import gwtscalademo.client.GreetingService
import com.google.gwt.user.server.rpc.RemoteServiceServlet
class GreetingServiceImpl
extends RemoteServiceServlet
with GreetingService {
def greetServer(input:String):String = {
return "Hello from Scala, " + input + "!"
}
}
Note that while in Java, we'd have said
implements
, in Scala we say
with
. As I understand it, this means that Java interfaces get mapped onto
Scala traits. Hip!
Now you should be able to run the app locally -- right-click your project and select
Run As > Web Application. Once you've
learned Scala and made it do something interesting (you're on your own there), you can deploy it to App Engine!
Woo!