wicket and scala

Download Wicket and Scala

If you can't read please download the document

Post on 11-Nov-2014

114 views

Category:

Documents

6 download

Embed Size (px)

DESCRIPTION

Tutorial Wicket And Scala

TRANSCRIPT

Basic Wicket and ScalaDaan van Ettenhttp://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 Etteneer n i g n E e Softwar

@work

Daan van Etteneer 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

ScalaStatically typed

ScalaType inferencevar foo = 8 foo = bartype mismatch; found: String("bar") required: Int

ScalaEvery value is an objectvar foo = 8 def bar(a: String)= println(a)

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

Scala

Compiles to Java bytecode

Scala

Runs on the Java VM

Scala

ScalableFrom small scripts to large systems

Scala

Hello, World!

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

Scala

20 seconds

Scala

Scala

Scala

Scala

Functional concepts

ScalaDesign goals: Embrace immutability Avoid state

ScalaCleaner codeMore ne-grained reuse No Iterator loops needed :-)

ScalaBetter optimizationsMulti-core! Lazy evaluation Recursion

ScalaWhat about the functions?

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

ScalaFunction nestingdef function1(x : Int) = { def function2() = { println(x) } function2() }

ScalaFirst-class functionsdef foo (cb: ()=>Unit): Unit = { while (true) { cb(); Thread.sleep(1000); } } foo(Unit : println("hi"))

ScalaFirst-class functions in librariesval numbers = List(2,5,8,9) numbers.foreach( (x: Int) => print(x) )

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

ScalaPartially applied functionsdef 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"))) }

+ Hello, Wicket World! What's your name?
Your name is:

+

?

+

EASY REUSABLE NON-INTRUSIVE SAFE EFFICIENT SCALABLE

EASY

EASYPOJO-centric

EASYAll code in Java

EASYAll code in Java or Scala

EASYMaximum type safety and compile-time problem diagnosis

EASYMinimum reliance on special tools

EASY

REUSABLE

REUSABLEFunction reuse

REUSABLE

NON-INTRUSIVE

NON-INTRUSIVEHTML or other markup not polluted with programming semantics

NON-INTRUSIVEBut... Scala != Java

NON-INTRUSIVE

?

SAFE

SAFECode is secure by default

SAFEAll logic in Java (or Scala) with maximum type safety

SAFE

EFFICIENT / SCALABLE

EFFICIENT / SCALABLEE cient and lightweight

EFFICIENT / SCALABLEScala means reducing the amount of code.

EFFICIENT / SCALABLE

EASY REUSABLE NON-INTRUSIVE SAFE EFFICIENT SCALABLE

maven

mavenmaven-scala-plugin

mavenUnder org.scala-tools maven-scala-plugin compile testCompile

mavenUnder

scala Scala Tools http://scala-tools.org/repo-releases true false

mavenUnder

scala Scala Tools http://scala-tools.org/repo-releases true false

mavenUnder org.scala-lang scala-library 2.7.3

mavenYou can add this to any Java+Maven project!

mavenProject layout

mavenpom.xml src main java test java

mavenpom.xml src main scala test scala

mavenpom.xml src main java scala test java scala

mavenHello, Wicket World! built in Maven

maven

maven

Download the demo project.

Java Scala

Java Scala

Java interface implemented in Scala

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

Java Scalapublic 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 Scalaextending 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 Scalapublic interface SomeInterface { void doSomething(String argument); } trait SomeTrait { def doSomething=(argument:String):Unit }

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

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

Java Scalapublic 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 Scalapublic interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface { def doSomething=(argument:String):Unit {...} } public class Other extends SomeImplementation { ... }

Java Scala

maven-scala-pluginHandles circular dependencies!

Java Scala

How?

Java Scala

scalacparses Java code(since 2.7.2)

Java Scalapublic 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 Javahttp://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 openingshttp://scala.sygneca.com/

O cial mailing lists Subscribe: empty message to scala-subscribe@listes.ep .chhttp://www.scala-lang.org/node/199

All samples can be downloaded at

http://stuq.nl

Get started with Scala and Wicket!

Thanks!