Wicket and Scala
-
Upload
andre-white -
Category
Documents
-
view
152 -
download
6
description
Transcript of Wicket and Scala
Daan van Ettenhttp://stuq.nl
Amsterdam, March 24, 2009
Basic Wicket and Scala
Hi!
Goal:
After this talk, you can start coding Wicket in Scala.
Overview
Scala
ScalaWhat is
?
Hello, World!
Functional concepts
+
?
Simple project
maven
Java Scala
Who am I?
Daan van Etten
Daan van Etten
Software Engineer
Software Engineer
Daan van Etten
@work
Software Engineer
Daan van Etten
Let’s begin
ScalaWhat is
?
Scala
History
Scala
1958
Scala
Martin Odersky
Scala
Scala
Compilers
Scala
Functional languages
(more about that later)
Scala
Scala
GenericJava
Scala
Scala
Java 5Generics
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 = 8foo = “bar”
type mismatch;found: String("bar")required: Int
Scala
Every value is an object
var foo = 8def bar(a: String)= println(a)
1 + 3 - 61.+(3).-(6)
Scala
Every operation is a method call
Scala
Compiles toJava bytecode
Scala
Runs on theJava VM
Scala
ScalableFrom 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
ScalaDesign goals:
Embrace immutabilityAvoid state
Scala
Cleaner code
More !ne-grained reuseNo Iterator loops needed :-)
Scala
Better optimizationsMulti-core!
Lazy evaluationRecursion
Scala
What aboutthe functions?
1 + 3 - 61.+(3).-(6)
Scala
Every operation is a method call
Scala
def function1(x : Int) = { def function2() = { println(x) } function2()}
Function nesting
Scala
def foo (cb: ()=>Unit): Unit = { while (true) { cb(); Thread.sleep(1000); }}
foo(Unit : println("hi"))
First-class functions
First-class functions in libraries
Scala
val numbers = List(2,5,8,9)numbers.foreach( (x: Int) => print(x) )
Anonymous functions
Scala
((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>
+
+
?
+
EASYREUSABLE
NON-INTRUSIVESAFE
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
REUSABLE
Function reuse
REUSABLE
NON-INTRUSIVE
HTML or other markupnot polluted with
programming semantics
NON-INTRUSIVE
But... Scala != Java
NON-INTRUSIVE
NON-INTRUSIVE
?
SAFE
Code is secure by default
SAFE
SAFEAll logic in Java (or Scala)
with maximum type safety
SAFE
EFFICIENT / SCALABLE
E!cient and lightweight
EFFICIENT / SCALABLE
Scala means reducingthe amount of code.
EFFICIENT / SCALABLE
EFFICIENT / SCALABLE
EASYREUSABLE
NON-INTRUSIVESAFE
EFFICIENT SCALABLE
maven
maven-scala-plugin
maven
mavenUnder <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>
mavenUnder <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>
mavenUnder <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>
mavenUnder <dependencies>
<dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.7.3</version></dependency>
maven
You can add this to anyJava+Maven project!
maven
Project layout
maven
src
pom.xml
main
java
test
java
maven
src
pom.xml
main
scala
test
scala
maven
src
pom.xml
main
java
test
java
scala
scala
maven
Hello, Wicket World!built in Maven
maven
maven
Download thedemo project.
Java Scala
Java Scala
Java interfaceimplemented in Scala
Java Scala
public interface SomeInterface { void doSomething(String argument);}
class SomeImplementation extends SomeInterface { def doSomething(argument: String):Unit = { println(argument) }}
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
class SomeImplementation extends SomeInterface { def doSomething(argument: String) : unit = { println(argument) }}
extending a trait
Java Scala
interface != trait
Java Scala
traits can havemethod
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
class SomeImplementation extends SomeInterface { def doSomething=(argument:String):Unit {...}}
public interface SomeInterface { void doSomething(String argument);}
Java Scala
public class Other extends SomeImplementation { ...}
Handles circular
dependencies!
Java Scala
maven-scala-plugin
Java Scala
How?
Java Scala
scalac
parses Java code(since 2.7.2)
class SomeImplementation extends SomeInterface { def doSomething=(argument:String):Unit {...}}
public interface SomeInterface { void doSomething(String argument);}
Java Scala
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 afeature by feature comparison
of Scala and Java
http://blogs.sun.com/sundararajan/entry/scala_for_java_programmers
Series of 6 great articlescovering 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 [email protected]#.ch
http://www.scala-lang.org/node/199
Get started with Scala and Wicket!
Thanks!