You are on page 1of 146

Basic Wicket and Scala

Daan van Etten


http://stuq.nl

Amsterdam, March 24, 2009

Hi!

Goal:
After this talk, you can start coding Wicket in Scala.

Overview

Scala

What is

Scala ?

Hello, World!

Functional concepts

Simple project

maven

Java Scala

Who am I?

Daan van Etten

Daan van Etten

Soft

r e e n i g n E e r a w

Daan van Etten


eer n i g n E e Softwar

@work

Daan van Etten


eer n i g n E e Softwar

Lets begin

What is

Scala ?

Scala

History

Scala

1958

Scala

Martin Odersky

Scala

Scala

Compilers

Scala

Functional languages
(more about that later)

Scala

Scala

Generic Java

Scala

Scala

Java 5 Generics

Scala

New javac

Scala

2001

Scala

Scala

Scala

First release

Scala

2003

Scala

Object Oriented

Scala

Functional

Scala
Statically typed

Scala
Type inference
var foo = 8 foo = bar
type mismatch; found: String("bar") required: Int

Scala
Every value is an object
var foo = 8 def bar(a: String)= println(a)

Scala
Every operation is a method call 1 + 3 - 6 1.+(3).-(6)

Scala

Compiles to Java bytecode

Scala

Runs on the Java VM

Scala

Scalable
From small scripts to large systems

Scala

Hello, World!

Scala
object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } }

Scala

20 seconds

Scala

Scala

Scala

Scala

Functional concepts

Scala
Design goals: Embrace immutability Avoid state

Scala
Cleaner code
More ne-grained reuse No Iterator loops needed :-)

Scala
Better optimizations
Multi-core! Lazy evaluation Recursion

Scala
What about the functions?

Scala
Every operation is a method call 1 + 3 - 6 1.+(3).-(6)

Scala
Function nesting
def function1(x : Int) = { def function2() = { println(x) } function2() }

Scala
First-class functions
def foo (cb: ()=>Unit): Unit = { while (true) { cb(); Thread.sleep(1000); } } foo(Unit : println("hi"))

Scala
First-class functions in libraries
val numbers = List(2,5,8,9) numbers.foreach( (x: Int) => print(x) )

Scala
Anonymous functions
((i:Int, j:Int) => i + j)(3, 4) Java: int calc(int i, int j){ return i + j; }; calc(3, 4);

Scala
Partially applied functions
def calc(x:Int, y:Int, z:Int)=x+y+z val calcPart = calc(1, _:Int, 3) calcPart(4)

Simple project

Hello, Wicket World!

+
object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } }

+
class HelloWorld extends WebApplication { def main(args: Array[String]) { println("Hello, world!") } }

+
class HelloWorld extends WebApplication { }

+
class HelloWorld extends WebApplication { def getHomePage = classOf[HomePage] }

+
class HelloWorld extends WebApplication { def getHomePage = classOf[HomePage] } class HomePage extends WebPage {

+
class HelloWorld extends WebApplication { def getHomePage = classOf[HomePage] } class HomePage extends WebPage { var name = "" val form = new Form("form") add(form) form.add(new TextField("name", new PropertyModel(this, "name"))) form.add(new Label("helloworld", new PropertyModel(this, "name"))) }

+
<body> <p><b>Hello, Wicket World!</b></p> <form wicket:id="form"> What's your name? <br/> <input wicket:id="name"/> <input type="submit" value = "OK" /> <p>Your name is: <b><span wicket:id="helloworld"/></b> </p> </form> </body>

EASY REUSABLE NON-INTRUSIVE SAFE EFFICIENT SCALABLE

EASY

EASY
POJO-centric

EASY
All code in Java

EASY
All code in Java or Scala

EASY
Maximum type safety and compile-time problem diagnosis

EASY
Minimum reliance on special tools

EASY

REUSABLE

REUSABLE
Function reuse

REUSABLE

NON-INTRUSIVE

NON-INTRUSIVE
HTML or other markup not polluted with programming semantics

NON-INTRUSIVE
But... Scala != Java

NON-INTRUSIVE

SAFE

SAFE
Code is secure by default

SAFE
All logic in Java (or Scala) with maximum type safety

SAFE

EFFICIENT / SCALABLE

EFFICIENT / SCALABLE
E cient and lightweight

EFFICIENT / SCALABLE
Scala means reducing the amount of code.

EFFICIENT / SCALABLE

EASY REUSABLE NON-INTRUSIVE SAFE EFFICIENT SCALABLE

maven

maven
maven-scala-plugin

maven
Under <build> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin>

maven
Under <pluginRepositories>

<pluginRepository> <id>scala</id> <name>Scala Tools</name> <url>http://scala-tools.org/repo-releases</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository>

maven
Under <repositories>

<repository> <id>scala</id> <name>Scala Tools</name> <url>http://scala-tools.org/repo-releases</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository>

maven
Under <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.7.3</version> </dependency>

maven
You can add this to any Java+Maven project!

maven
Project layout

maven
pom.xml src main java test java

maven
pom.xml src main scala test scala

maven
pom.xml src main java scala test java scala

maven
Hello, Wicket World! built in Maven

maven

maven

Download the demo project.

Java Scala

Java Scala

Java interface implemented in Scala

Java Scala
public interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface { def doSomething(argument: String):Unit = { println(argument) } }

Java Scala
public interface SomeInterface { void doSomething(String argument); }

class SomeImplementation extends SomeInterface { def doSomething(argument: String):unit = { println(argument) } }

Java Scala

Scala does not have interfaces!

Java Scala

Scala has traits

Java Scala
extending a trait

class SomeImplementation extends SomeInterface { def doSomething(argument: String) : unit = { println(argument) } }

Java Scala

interface != trait

Java Scala

traits can have method implementations

Java Scala
public interface SomeInterface { void doSomething(String argument); } trait SomeTrait { def doSomething=(argument:String):Unit }

Java Scala
public interface SomeInterface { void doSomething(String argument); } trait SomeTrait { def doSomething(argument:String):Unit def computeSomething = title.length * 10 }

Java Scala
public interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface { def doSomething(argument: String):Unit = { println(argument) } }

Java Scala
public interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface {

Java Scala

Error: class SomeImplementation needs to be abstract, since method doSomething in trait SomeInterface of type (java.lang.String)Unit is not defined.

Java Scala

Java and Scala combined: Circular dependencies

Java Scala
public interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface { def doSomething=(argument:String):Unit {...} } public class Other extends SomeImplementation { ... }

Java Scala

maven-scala-plugin
Handles circular dependencies!

Java Scala

How?

Java Scala

scalac
parses Java code
(since 2.7.2)

Java Scala
public interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface { def doSomething=(argument:String):Unit {...} } public class Other extends SomeImplementation { ... }

Scala home on the web. Reference manuals, tutorials, news, speci cations.
http://www.scala-lang.org

Interpreter, variables, methods, loops, arrays, lists, tuples, sets, maps, classes, singletons, traits, mixins.
http://www.artima.com/scalazine/articles/steps.html

Multiple articles covering a feature by feature comparison of Scala and Java


http://blogs.sun.com/sundararajan/entry/scala_for_java_programmers

Series of 6 great articles covering a lot of Scala. Aimed at Java developers.


http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees

Scala Wiki. FAQ, code samples, design patterns, Scala job openings
http://scala.sygneca.com/

O cial mailing lists Subscribe: empty message to scala-subscribe@listes.ep .ch


http://www.scala-lang.org/node/199

All samples can be downloaded at

http://stuq.nl

Get started with Scala and Wicket!

Thanks!

You might also like