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 .

Let’s 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 .

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

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

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

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 .+(3).-(6) .6 1.

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

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

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

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

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

+ .

+ Simple project .

+ Hello. Wicket World! .

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

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

+ class HelloWorld extends WebApplication { } .

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

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

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

+ <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.7.3</version> </dependency> .scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.

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

maven Project layout .

xml src main java test java .maven pom.

maven pom.xml src main scala test scala .

maven pom.xml src main java scala test java scala .

Wicket World! built in Maven .maven Hello.

maven .

maven .

.Download the demo project.

Java Scala .

Java Scala Java interface implemented in Scala .

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

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

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

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

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 .

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

Java Scala maven-scala-plugin Handles circular dependencies! .

Java Scala How? .

7.2) .Java Scala scalac parses Java code (since 2.

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

.

.

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

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

Multiple articles covering a feature by feature comparison of Scala and Java http://blogs.com/sundararajan/entry/scala_for_java_programmers .sun.

http://www. Aimed at Java developers.codecommit.Series of 6 great articles covering a lot of Scala.com/blog/scala/roundup-scala-for-java-refugees .

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

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

All samples can be downloaded at http://stuq.nl .

Get started with Scala and Wicket! .

Thanks! .