Wicket and Scala

Download Wicket and Scala

Post on 11-Nov-2014

112 views

Category:

Documents

6 download

Embed Size (px)

DESCRIPTION

Tutorial Wicket And Scala

TRANSCRIPT

<p>Basic Wicket and ScalaDaan van Ettenhttp://stuq.nl</p> <p>Amsterdam, March 24, 2009</p> <p>Hi!</p> <p>Goal:After this talk, you can start coding Wicket in Scala.</p> <p>Overview</p> <p>Scala</p> <p>What is</p> <p>Scala ?</p> <p>Hello, World!</p> <p>Functional concepts</p> <p>+</p> <p>?</p> <p>Simple project</p> <p>maven</p> <p>Java Scala</p> <p>Who am I?</p> <p>Daan van Etten</p> <p>Daan van Etten</p> <p>Soft</p> <p>r e e n i g n E e r a w</p> <p>Daan van Etteneer n i g n E e Softwar</p> <p>@work</p> <p>Daan van Etteneer n i g n E e Softwar</p> <p>Lets begin</p> <p>What is</p> <p>Scala ?</p> <p>Scala</p> <p>History</p> <p>Scala</p> <p>1958</p> <p>Scala</p> <p>Martin Odersky</p> <p>Scala</p> <p>Scala</p> <p>Compilers</p> <p>Scala</p> <p>Functional languages(more about that later)</p> <p>Scala</p> <p>Scala</p> <p>Generic Java</p> <p>Scala</p> <p>Scala</p> <p>Java 5 Generics</p> <p>Scala</p> <p>New javac</p> <p>Scala</p> <p>2001</p> <p>Scala</p> <p>Scala</p> <p>Scala</p> <p>First release</p> <p>Scala</p> <p>2003</p> <p>Scala</p> <p>Object Oriented</p> <p>Scala</p> <p>Functional</p> <p>ScalaStatically typed</p> <p>ScalaType inferencevar foo = 8 foo = bartype mismatch; found: String("bar") required: Int</p> <p>ScalaEvery value is an objectvar foo = 8 def bar(a: String)= println(a)</p> <p>ScalaEvery operation is a method call 1 + 3 - 6 1.+(3).-(6)</p> <p>Scala</p> <p>Compiles to Java bytecode</p> <p>Scala</p> <p>Runs on the Java VM</p> <p>Scala</p> <p>ScalableFrom small scripts to large systems</p> <p>Scala</p> <p>Hello, World!</p> <p>Scalaobject HelloWorld { def main(args: Array[String]) { println("Hello, world!") } }</p> <p>Scala</p> <p>20 seconds</p> <p>Scala</p> <p>Scala</p> <p>Scala</p> <p>Scala</p> <p>Functional concepts</p> <p>ScalaDesign goals: Embrace immutability Avoid state</p> <p>ScalaCleaner codeMore ne-grained reuse No Iterator loops needed :-)</p> <p>ScalaBetter optimizationsMulti-core! Lazy evaluation Recursion</p> <p>ScalaWhat about the functions?</p> <p>ScalaEvery operation is a method call 1 + 3 - 6 1.+(3).-(6)</p> <p>ScalaFunction nestingdef function1(x : Int) = { def function2() = { println(x) } function2() }</p> <p>ScalaFirst-class functionsdef foo (cb: ()=&gt;Unit): Unit = { while (true) { cb(); Thread.sleep(1000); } } foo(Unit : println("hi"))</p> <p>ScalaFirst-class functions in librariesval numbers = List(2,5,8,9) numbers.foreach( (x: Int) =&gt; print(x) )</p> <p>ScalaAnonymous functions((i:Int, j:Int) =&gt; i + j)(3, 4) Java: int calc(int i, int j){ return i + j; }; calc(3, 4);</p> <p>ScalaPartially applied functionsdef calc(x:Int, y:Int, z:Int)=x+y+z val calcPart = calc(1, _:Int, 3) calcPart(4)</p> <p>+</p> <p>+</p> <p>Simple project</p> <p>+</p> <p>Hello, Wicket World!</p> <p>+object HelloWorld { def main(args: Array[String]) { println("Hello, world!") } }</p> <p>+class HelloWorld extends WebApplication { def main(args: Array[String]) { println("Hello, world!") } }</p> <p>+class HelloWorld extends WebApplication { }</p> <p>+class HelloWorld extends WebApplication { def getHomePage = classOf[HomePage] }</p> <p>+class HelloWorld extends WebApplication { def getHomePage = classOf[HomePage] } class HomePage extends WebPage {</p> <p>}</p> <p>+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"))) }</p> <p>+ <b>Hello, Wicket World!</b> What's your name? <br /> Your name is: </p> <p>+</p> <p>?</p> <p>+</p> <p>EASY REUSABLE NON-INTRUSIVE SAFE EFFICIENT SCALABLE</p> <p>EASY</p> <p>EASYPOJO-centric</p> <p>EASYAll code in Java</p> <p>EASYAll code in Java or Scala</p> <p>EASYMaximum type safety and compile-time problem diagnosis</p> <p>EASYMinimum reliance on special tools</p> <p>EASY</p> <p>REUSABLE</p> <p>REUSABLEFunction reuse</p> <p>REUSABLE</p> <p>NON-INTRUSIVE</p> <p>NON-INTRUSIVEHTML or other markup not polluted with programming semantics</p> <p>NON-INTRUSIVEBut... Scala != Java</p> <p>NON-INTRUSIVE</p> <p>?</p> <p>SAFE</p> <p>SAFECode is secure by default</p> <p>SAFEAll logic in Java (or Scala) with maximum type safety</p> <p>SAFE</p> <p>EFFICIENT / SCALABLE</p> <p>EFFICIENT / SCALABLEE cient and lightweight</p> <p>EFFICIENT / SCALABLEScala means reducing the amount of code.</p> <p>EFFICIENT / SCALABLE</p> <p>EASY REUSABLE NON-INTRUSIVE SAFE EFFICIENT SCALABLE</p> <p>maven</p> <p>mavenmaven-scala-plugin</p> <p>mavenUnder org.scala-tools maven-scala-plugin compile testCompile </p> <p>mavenUnder </p> <p> scala Scala Tools http://scala-tools.org/repo-releases true false </p> <p>mavenUnder </p> <p> scala Scala Tools http://scala-tools.org/repo-releases true false </p> <p>mavenUnder org.scala-lang scala-library 2.7.3 </p> <p>mavenYou can add this to any Java+Maven project!</p> <p>mavenProject layout</p> <p>mavenpom.xml src main java test java</p> <p>mavenpom.xml src main scala test scala</p> <p>mavenpom.xml src main java scala test java scala</p> <p>mavenHello, Wicket World! built in Maven</p> <p>maven</p> <p>maven</p> <p>Download the demo project.</p> <p>Java Scala</p> <p>Java Scala</p> <p>Java interface implemented in Scala</p> <p>Java Scalapublic interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface { def doSomething(argument: String):Unit = { println(argument) } }</p> <p>Java Scalapublic interface SomeInterface { void doSomething(String argument); }</p> <p>?</p> <p>class SomeImplementation extends SomeInterface { def doSomething(argument: String):unit = { println(argument) } }</p> <p>Java Scala</p> <p>Scala does not have interfaces!</p> <p>Java Scala</p> <p>Scala has traits</p> <p>Java Scalaextending a trait</p> <p>class SomeImplementation extends SomeInterface { def doSomething(argument: String) : unit = { println(argument) } }</p> <p>Java Scala</p> <p>interface != trait</p> <p>Java Scala</p> <p>traits can have method implementations</p> <p>Java Scalapublic interface SomeInterface { void doSomething(String argument); } trait SomeTrait { def doSomething=(argument:String):Unit }</p> <p>Java Scalapublic interface SomeInterface { void doSomething(String argument); } trait SomeTrait { def doSomething(argument:String):Unit def computeSomething = title.length * 10 }</p> <p>Java Scalapublic interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface { def doSomething(argument: String):Unit = { println(argument) } }</p> <p>Java Scalapublic interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface {</p> <p>}</p> <p>Java Scala</p> <p>Error: class SomeImplementation needs to be abstract, since method doSomething in trait SomeInterface of type (java.lang.String)Unit is not defined.</p> <p>Java Scala</p> <p>Java and Scala combined: Circular dependencies</p> <p>Java Scalapublic interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface { def doSomething=(argument:String):Unit {...} } public class Other extends SomeImplementation { ... }</p> <p>Java Scala</p> <p>maven-scala-pluginHandles circular dependencies!</p> <p>Java Scala</p> <p>How?</p> <p>Java Scala</p> <p>scalacparses Java code(since 2.7.2)</p> <p>Java Scalapublic interface SomeInterface { void doSomething(String argument); } class SomeImplementation extends SomeInterface { def doSomething=(argument:String):Unit {...} } public class Other extends SomeImplementation { ... }</p> <p>Scala home on the web. Reference manuals, tutorials, news, speci cations.http://www.scala-lang.org</p> <p>Interpreter, variables, methods, loops, arrays, lists, tuples, sets, maps, classes, singletons, traits, mixins.http://www.artima.com/scalazine/articles/steps.html</p> <p>Multiple articles covering a feature by feature comparison of Scala and Javahttp://blogs.sun.com/sundararajan/entry/scala_for_java_programmers</p> <p>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</p> <p>Scala Wiki. FAQ, code samples, design patterns, Scala job openingshttp://scala.sygneca.com/</p> <p>O cial mailing lists Subscribe: empty message to scala-subscribe@listes.ep .chhttp://www.scala-lang.org/node/199</p> <p>All samples can be downloaded at</p> <p>http://stuq.nl</p> <p>Get started with Scala and Wicket!</p> <p>Thanks!</p>