Published on

Can Scala replace Java in the near future?

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Processorerna blir inte snabbare snarare tvärom. Dock gäller Moores lag fortfarande (2x transistorer på 18 mån). Problem med klockfrekvens Amhdals lag - > hur mycket snabbare blir applikationen när du tillför nya resursen (processorer etc). En app kan inte bli snabbare än sin minst paralelliserbara del.
  • Functins are defined with ’def’
  • Har visat sig att statisk kompilerade språk inte ger den fördel man trott - > inte så många rte som ske dock mycket bra med dynamisk typade språk
  • 20 testers per programmer ~1 200 0000 Other types of concurrency, transaction based memory, quasi-static scheduling, AOP How many of you can write thread safe code
  • Scala

    1. 1. Andreas Enbohm 2009-03-15 Scala or ” The Free Lunch is Over”
    2. 2. Why Scala <ul><li>The free lunch is over... </li></ul><ul><li>Today – 2 Cores </li></ul><ul><li>2010 maybe 12 (AMD), in 5 years maybe 16 </li></ul><ul><li>Difficult to utilize several CPUs with Java </li></ul><ul><li>Huge API in current Java – some argue it may collape due to its own weight </li></ul><ul><li>Scala = ScalableLanguage </li></ul><ul><li>Designed to fit todays and some of tomorrows programming paradigms </li></ul><ul><li>Actors – Easier to utilize than memory synchronization (more about this later) </li></ul><ul><li>Nice (less boiler plate code) syntax and ’type safe duck typing’ </li></ul><ul><li>And much more... </li></ul>
    3. 3. Scala - Features <ul><li>OO and functional language ” a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way ” </li></ul><ul><li>Functional language – (often) no side effect, avoids state and mutable data, i.e functions can run independently and without knowlegde of eachother </li></ul><ul><li>Open Source </li></ul><ul><li>Compact code, ca 50 % less code than Java </li></ul><ul><li>Static typed language (but with ’kind of’ duck typing, more about this later) </li></ul><ul><li>Pattern matching, singel inheritance, immutable collections (like Java Strings), XML as FCC </li></ul><ul><li>SUnit, Lift (web framework), Support for Maven, Ant, Plugins for Eclipse, Netbeans, IntelliJ </li></ul><ul><li>Why not Ruby, Groovy, F#? - Fully interoperable with Java (call Java classes whenever you want) - Refactoring (static typed) - runs on world’s best VM! - very simple to inherit from Java classes - Javas annotations, threads, for each, it all works - Very fast (600x faster than groovy, lift 6x faster than Rails?) </li></ul>def sum(a:Int,b:Int) = a+b
    4. 4. Scala – Compared to Java <ul><li>Defining a class </li></ul><ul><li>Everything in Scala is an Object! This also includes functions </li></ul>class Person (var firstName: String, var lastName: String) { } public class Person { private String firstName; private String lastName; public Person(String firstName, String lastName) { this .firstName = firstName; this .lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this .firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this .lastName = lastName; } }
    5. 5. Scala – Compared to Java <ul><li>Defining variables - ; and type can often be omitted </li></ul><ul><li>Creating objects </li></ul><ul><li>List is of type List[String] by default </li></ul><ul><li>List objects in Scala contains several useful functions to access elements in the List </li></ul><ul><li>Ommiting some dots… </li></ul>val myInt: Int = 1; val anotherInt = 2 val anObject = Object //Omit () if you want just ’cluttering’ your code val aList = List(”John Doe”, ”Jane Doe”, ”Baby Doe”) println (&quot;Hi, &quot; +aList.tail.head) //Prints “Hi, Jane Doe” List<String> aList = new ArrayList<String>(); //Java syntax for creating a list-object val aList = List //Scala syntax for creating corresponding list val aList = List[String] //Also works, notice just ONE generic definiton import java.util.{Date,Locale} import java.text.DateFormat import java.text.DateFormat._ val now = new Date val df = getDateInstance(LONG, Locale.FRANCE) println ( df format now) //same as df.format(now)
    6. 6. Scala – Compared to Java <ul><li>Exceptions - Scala has no checked exception (unlike Java but like C#) - If used with Java, use annotation @throws(classOf[IOException]) </li></ul><ul><li>Using pattern matching and functions </li></ul><ul><li>Scala uses ’ traits’ which can be compared to interfaces. Can have implementations (but no state) </li></ul>try { doSomething(params) } catch { case ex: IOException => println(&quot;Oops! Some file is probably missing&quot;) case ex: NullPointerException => println(“Dohh!! Must initialize the variable &quot;) } object MatchTest1 extends Application { def matchTest(x: Int): String = x match { case 1 => &quot;one&quot; case 2 => &quot;two&quot; case _ => &quot;many&quot; } println (matchTest(1)) //Prints “one” println (matchTest(99)) //Prints “many” }
    7. 7. Duck Typing in Scala <ul><li>” If it walks like a duck and quacks like a duck, then I would call it a duck.” – Wise Guy </li></ul><ul><li>Statically typed language don’t offer this flexibility (Java, C#) - i.e. no ’Duck Typing’ </li></ul><ul><li>Downside – what happens if object does not have a ’quack’-method -> RuntimeException! </li></ul><ul><li>In Java, we must add an interface with method ’quack()’ – impossible to add an interface without changing the class! </li></ul>class Duck { public void quack() { print(”Duck Quacking!”) } class Person { public void quack() { print(”Person Quacking!”) } } Duck d = new Duck(); Person p = new Person(); testQuack(d); testQucak(p); testQuack(duckableObject) { duckableObject.quack(); //Lets hope this work!!! }
    8. 8. Duck Typing in Scala <ul><li>However Scala offers ’Structual Typing’ – a.k.a. Type safe duck typing </li></ul><ul><li>Consider following example (Scala syntax) </li></ul><ul><li>Scala offers structual typing, i.e. def getName() in test </li></ul><ul><li>The structual type ’getName()’ checks at compile time that ’aFileObject’ has a getName() method if not – compile error </li></ul><ul><li>If getName() is needed more than one, use traits. </li></ul>class File (name: String) { def getName(): String = name def open() { /*..*/ } def close() { println(&quot;close file&quot;) } } def testPrintName (aFileObject: { def getName(): String }) { println (aFileObject.getName) } testPrintName (new File(&quot;test.txt&quot;)) testPrintName (new;test.txt&quot;)) trait HasName { def getName() : String } def testPrintName (HasName f) = { println(f.getName) }
    9. 9. Actors <ul><li>” Don't bring the grape juice into the living room“ – Brian Goetz </li></ul><ul><li>Java threads - shared memory, i.e. only one thread can operate on a shared resource concurrently - expensive to create, i.e. every thread has a high memory consumption - context switch. i.e a Java thread maps to a OS thread (limited numbers ~3K) - context switch make page faults - > slow due to thread needs to re-read data from RAM - difficult to use, i.e. deadlocks, raise conditions, live locks, NASA Mars explorer experied live lock - synchronize, i.e. not just a a monitor/lock but also write to main memory (no registers) ~100 slower! - Java was designed for single processors, not multi-cores! </li></ul><ul><li>Actor based concurrency - concurrenct processes communicates by exchanging messages - asynchronous message passing - ’share-nothing’-model - Erlang early to implement this style of concurrency - Erlang based web server ~80000 clients, Apache ~4000 clients </li></ul><ul><li>Two flavours in Scala; - Thread based actors – high level abstraction of threads which replaces error-prone shared memory access - Thread less actors – offers enourmous scalability. Lightweight processes are used as actors with much less overhead than a usual thread. ’Piggy-backs’ on calling threads stack trace (continuation closure) </li></ul>
    10. 10. Downsides <ul><li>” With great power comes great complexity” – James Gosling </li></ul><ul><li>New languages difficult to introduce to organisations </li></ul><ul><li>Not ready for prime time? </li></ul>
    11. 11. Questions? <ul><li> </li></ul>