Scala - brief intro

4,663 views

Published on

simple intro to scala for java folks

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,663
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
74
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Scala - brief intro

  1. 1. Strengths of Scala Java replacement? The answer doesn’t matter! Brief intro to Scala - Razvan Cojocaru – Nov’08 & Mar’09
  2. 2. Overview <ul><li>Brief syntax – at least 30% off Java </li></ul><ul><ul><li>Smart compiler != dumb programmer </li></ul></ul><ul><ul><li>Compiles into bytecode, runs on JVM  access to all Java libraries and, more importantly, APIs </li></ul></ul><ul><li>C++ is back! …mean as ever! </li></ul><ul><ul><li>Multiple inheritance, operator overloading, true generics, pass functions around, simple syntax </li></ul></ul><ul><li>Scala is better: true multi-paradigm </li></ul><ul><ul><li>Functional programming </li></ul></ul><ul><ul><li>Scalable language </li></ul></ul><ul><li>Not for the faint of heart! </li></ul>
  3. 3. Smart Compiler <ul><li>Makes up constructors, getX/setX stuff </li></ul><ul><ul><li>class Point (x:Int,y:Int) </li></ul></ul><ul><li>Figures out types for values/variables and functions etc </li></ul><ul><ul><li>def getX = x </li></ul></ul><ul><li>Lots of shortcuts in the syntax, i.e no {} for method bodies </li></ul>
  4. 4. Smart compiler != dumb programmer <ul><li>Class Point { </li></ul><ul><li>int x; </li></ul><ul><li>int y; </li></ul><ul><li>public Point (int ax, int ay) { </li></ul><ul><li>this.x = ax; </li></ul><ul><li>this.y = ay; </li></ul><ul><li>} </li></ul><ul><li>public int getX () { return x; } </li></ul><ul><li>public int getY () { return y; } </li></ul><ul><li>public int setX (int x) { </li></ul><ul><li>if (x <= 0) </li></ul><ul><li>throw new IllegalArgumentException(“blah”); </li></ul><ul><li>else </li></ul><ul><li>this.x=x } </li></ul><ul><li>public int setY (int y) { this.y=y } </li></ul><ul><li>} </li></ul><ul><li>Class Point ( var x:int, var y:int) </li></ul><ul><li>def x_= (ax:Int) = </li></ul><ul><li>require (ax > 0); x=ax </li></ul><ul><li>To overwrite the assign, is actually a little more complicated: </li></ul><ul><li>def + (p:Point) = </li></ul><ul><li>new Point (x+p.x, y+p.y) </li></ul>Guess who ^^^ Guess who ^^^
  5. 5. Constructor/field generation <ul><li>class SimplerPoint ( </li></ul><ul><li>private var ix : Int, </li></ul><ul><li>private var iy :Int) </li></ul><ul><li>{ </li></ul><ul><li>def x = ix </li></ul><ul><li>def y = iy </li></ul><ul><li>def x_= ( ax :Int) { require( ax >0); ix = ax } </li></ul><ul><li>def y_= ( ay :Int) { require( ay >0); iy = ay } </li></ul><ul><li>} </li></ul><ul><li>class Point (ax:Int, ay:Int) </li></ul><ul><li>{ </li></ul><ul><li>private[this] var ix :Int = ax </li></ul><ul><li>private[this] var iy :Int = ay </li></ul><ul><li>def x = ix </li></ul><ul><li>def y = iy </li></ul><ul><li>def x_= (ax:Int) { require(ax>0); ix =ax } </li></ul><ul><li>def y_= (ay:Int) { require(ay>0); iy =ay } </li></ul><ul><li>} </li></ul><ul><li>// check this out !!! </li></ul><ul><li>class Point (ax:Int, ay:Int) </li></ul><ul><li>{ </li></ul><ul><li>require(ax>0); // class body is in fact c-tor </li></ul><ul><li>private[this] var ix :Int = ax </li></ul><ul><li>def x_= (ax:Int) { require(ax>0); ix =ax } </li></ul><ul><li>} </li></ul>Ix/iy are both constructor and fields Classic: ax/ay are just args for constructor
  6. 6. Traits <ul><li>Like multiple inheritance </li></ul><ul><ul><li>More like “polymorphic composition”…if that pairing makes sense  </li></ul></ul><ul><li>Simplify coding a lot, for classes with multiple “traits”. </li></ul><ul><li>No interfaces </li></ul><ul><li>There are abstract classes, though </li></ul><ul><ul><li>An abstract trait (all methods abstract) is equivalent to a Java interface </li></ul></ul>
  7. 7. Traits - features <ul><li>As opposed to interfaces, traits can implement methods </li></ul><ul><ul><li>No “controllers” etc </li></ul></ul><ul><ul><li>No replication of code. </li></ul></ul><ul><li>With single inheritance, you decide on a base class and then REWRITE every time the methods of other implemented interfaces </li></ul><ul><ul><li>“ idiot controller” syndrome: keep algorthms in different classes, to avoid rewriting them, see next slide </li></ul></ul><ul><li>TODO: read about the “linearization” to avoid diamond inheritance </li></ul><ul><ul><li>Same as “mixin” in Ruby </li></ul></ul><ul><ul><li>Trait vs. Class ? Semantics … </li></ul></ul>
  8. 8. Traits vs interfaces <ul><li>interface Centered { </li></ul><ul><li>public Point getCenter () { return x; } </li></ul><ul><li>} </li></ul><ul><li>class IdiotController { </li></ul><ul><li>public void offset (Centered s, Point offsetBy) {…} </li></ul><ul><li>} </li></ul><ul><li>class MyShape extends Drawable implements Centered { </li></ul><ul><li>//… </li></ul><ul><li>public Point getCenter () { return c; } </li></ul><ul><li>} </li></ul><ul><li>//…in a java file far far away: </li></ul><ul><li>New IdiotController().offset (myShape, new Point (3,4)) </li></ul><ul><li>trait Centered () { </li></ul><ul><li>var center:Point </li></ul><ul><li>def offset (offsetBy:Point) = center += offsetBy </li></ul><ul><li>} </li></ul><ul><li>Class MyShape extends Drawable with Centered { </li></ul><ul><li>} </li></ul><ul><li>//…in a scala file anywereh in the galaxy </li></ul><ul><li>myShape.offset ((3,4)) </li></ul>Guess who ^^^ Guess who ^^^
  9. 9. The screwyness of it <ul><li>trait AnotherCentered { </li></ul><ul><li>def center:Point </li></ul><ul><li>def offset (offsetBy:Point) = center += offsetBy </li></ul><ul><li>} </li></ul><ul><li>class AnotherShape extends AnotherCentered { </li></ul><ul><li>override val center:Point = new Point(0,0) </li></ul><ul><li>} </li></ul><ul><li>// type parameters </li></ul><ul><li>Class MyContainer [T :> SomeBaseClass] { </li></ul><ul><li>var internal = new List[T] </li></ul><ul><li>def += (t:T) = internal += t </li></ul><ul><li>} </li></ul><ul><li>Values/variables and functions share the same namespace </li></ul><ul><li>Generics </li></ul><ul><ul><li>Uses [] instead of () </li></ul></ul><ul><ul><li>(use () instead of [] as well) </li></ul></ul><ul><li>Operator overloading </li></ul>
  10. 10. The screwyness of it (2) <ul><li>var greeting = &quot;“ </li></ul><ul><li>for (i <- 0 until args.length) </li></ul><ul><li>greeting += (args(i) + &quot; &quot;) </li></ul><ul><li>// It’s the same as: </li></ul><ul><li>val range = 0.until(args.length) </li></ul><ul><li>for (i <- range) </li></ul><ul><li>greeting += (args(i) + &quot; &quot;) </li></ul><ul><li>// should use this instead…remember Java </li></ul><ul><li>// Callback<T>? … ok…now forget it! </li></ul><ul><li>args.foreach (x => greetings += x) </li></ul><ul><li>expect (true) { </li></ul><ul><li>“ Samba pa ti” == new String (“Samba pa ti”) </li></ul><ul><li>} </li></ul><ul><li>. </li></ul><ul><li>For “for” is not “for” </li></ul><ul><li>But, “equals” is actually “equals” </li></ul>
  11. 11. Functional…ity <ul><li>Functional language – Lisp family </li></ul><ul><li>Function literals </li></ul><ul><li>Lambda stuff and currying </li></ul><ul><ul><li>It’s fashionable, sounds cool and it’s even useful sometimes… </li></ul></ul><ul><li>Syntactic sweetness </li></ul>
  12. 12. Functional…ity <ul><li>// a function typedef </li></ul><ul><li>type Fun = (String,String) => String </li></ul><ul><li>// a function literal assigned to a value/variable </li></ul><ul><li>val aFun = (x,y) => x+y </li></ul><ul><li>aFun ( aFun (“this”, “is”), “cool”) </li></ul><ul><li>// function literal passed to a function: </li></ul><ul><li>myArray.foreach ( x => println x ) </li></ul><ul><li>myArray.sort (<) </li></ul><ul><li>myArray.sort ( (x,y) => x < y ) </li></ul><ul><li>Functions themselves are objects! </li></ul><ul><li>You can pass them to other functions, </li></ul><ul><li>Assign to variables etc </li></ul><ul><li>… remember C++ pointers to functions) </li></ul>Guess who ^^^ Guess who ^^^
  13. 13. The Cool and the Screwy <ul><li>Abstract VAL implements a DEF </li></ul><ul><ul><li>… or vice-versa </li></ul></ul><ul><li>Partially applied functions </li></ul><ul><li>Extend the language syntax </li></ul><ul><li>Interpreted AND/OR compiled </li></ul><ul><ul><li>Can setup SCALA-only desktop environment with scripted etc… </li></ul></ul>
  14. 14. Quirks <ul><li>No statics </li></ul><ul><ul><li>funny implementation of singletons instead </li></ul></ul><ul><ul><li>… as “companion objects” </li></ul></ul><ul><li>Can’t define as many constructors as you want…use factories instead, which is also perfect usage of the singletons above… </li></ul>
  15. 15. Quirks <ul><li>// as a value </li></ul><ul><li>val abs = if (x < 0) 0–x else x </li></ul><ul><li>// or as a function – remember? </li></ul><ul><li>val absfun = (x:int) => if (x < 0) 0–x else x </li></ul><ul><li>myValue.asInstanceOf [OtherClass] </li></ul><ul><li>implicit def itos (x:Int) : String = String.valueOf(x) </li></ul><ul><li>// check this out: </li></ul><ul><li>def repeat[T] (n: Int) ( what: => T ): List[T] = ... </li></ul><ul><li>repeat(5) { println(“I will be quiet in class.&quot;))) } </li></ul><ul><li>Every statement block returns a value </li></ul><ul><li>Typecasts : </li></ul><ul><li>Implicit type conversions </li></ul><ul><li>Special syntax: </li></ul>Guess who ^^^ Guess who ^^^
  16. 16. Apply() and bound functions <ul><li>//simple OBJECT to time a statement… note this is not a class !!! apply() behaves differently </li></ul><ul><li>object Time { def apply[T] ( action: => T ): (T, Long) = { </li></ul><ul><li>startTimer() </li></ul><ul><li>val resp = action() </li></ul><ul><li>val time = stopTimer() </li></ul><ul><li>(resp, time) </li></ul><ul><li>} </li></ul><ul><li>// using it: </li></ul><ul><li>val (response, ms) = Time(Http.get(&quot;http://scala-blogs.org/&quot;)) </li></ul><ul><li>// it works because the http.get(xxx) above is a bound function not an actual call </li></ul><ul><li>Time(x) is defined via the apply() method </li></ul><ul><li>Special syntax to bind tuples </li></ul><ul><li>The Http.get() is NOT a call but a bound function </li></ul>Guess who ^^^ Guess who ^^^
  17. 17. UnitTests <ul><li>class TestRazElement extends JUnit3Suite { </li></ul><ul><li>def testA = </li></ul><ul><li>expect (&quot;roota&quot;) { doc a &quot;name&quot; } </li></ul><ul><li>def testXpe = </li></ul><ul><li>expect (&quot;11&quot;) { doc xpe &quot;/root/parent[@name='1']/child[@name='11']&quot; a &quot;name&quot; } </li></ul><ul><li>} </li></ul><ul><li>Several ways to write tests. Compatible with JUnit, NGUnit etc </li></ul>Guess who ^^^ Guess who ^^^
  18. 18. Apply() and bound functions <ul><li>//simple OBJECT to time a statement… note this is not a class !!! apply() behaves differently </li></ul><ul><li>object Time { def apply[T] ( action: => T ): (T, Long) = { </li></ul><ul><li>startTimer() </li></ul><ul><li>val resp = action() </li></ul><ul><li>val time = stopTimer() </li></ul><ul><li>(resp, time) </li></ul><ul><li>} </li></ul><ul><li>// using it: </li></ul><ul><li>val (response, ms) = Time(Http.get(&quot;http://scala-blogs.org/&quot;)) </li></ul><ul><li>// it works because the http.get(xxx) above is a bound function not an actual call </li></ul><ul><li>Time(x) is defined via the apply() method </li></ul><ul><li>Special syntax to bind tuples </li></ul><ul><li>The Http.get() is NOT a call but a bound function </li></ul>Guess who ^^^ Guess who ^^^
  19. 19. String pattern matching <ul><li>// note the triple double quotes “ “ “ </li></ul><ul><li>val pat = &quot;&quot;&quot;(w+):(w+) (.*)&quot;&quot;&quot;.r </li></ul><ul><li>// bind 3 vals at once </li></ul><ul><li>val pat(who, cmd, args) = “john:go someplace” </li></ul><ul><li>// or just iterate </li></ul><ul><li>for (s <- pat findAllIn input) </li></ul><ul><li>println(s) </li></ul><ul><li>Same patterns as java </li></ul><ul><li>Special syntax to bind multiple variables at once </li></ul>Guess who ^^^ Guess who ^^^
  20. 20. What Next? <ul><li>Read more (I like what the stuff the links on the next page point to). There’s a lot more to Scala than what I had the time and interest to show here! </li></ul><ul><li>Start playing with Scala in Eclipse (plugin kinda sucks) or NetBeans (great plugin, especially on a dark background) or vi(m). </li></ul><ul><li>Have Fun! </li></ul>
  21. 21. Links <ul><li>Good brief Scala intro: </li></ul><ul><ul><li>http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees </li></ul></ul><ul><li>The scala wiki: </li></ul><ul><ul><li>http:// scala.sygneca.com / </li></ul></ul><ul><li>Download new verions: </li></ul><ul><ul><li>http://www.scala-lang.org / </li></ul></ul><ul><li>More goodies: </li></ul><ul><ul><li>http://www.devoxx.com/download/attachments/1705916/D8_U_08_06_01.pdf </li></ul></ul><ul><ul><li>http://www.slideshare.net/michael.galpin/introduction-to-scala-for-java-developers-presentation </li></ul></ul><ul><ul><li>http://www.slideshare.net/jboner/pragmatic-real-world-scala-45-min-presentation </li></ul></ul>

×