Scala introduction


Published on

Published in: Technology
  • Be the first to comment

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

No notes for slide

Scala introduction

  1. 1. By : Zvika Markfeld Tikal Knowledge Scala TCE Introduction to The Scala Language Developer Productivity Redefined15-May-11
  2. 2. Agenda Introduction Language Features Ecosphere Q&A15-May-11 WWW.TIKALK.COM
  3. 3. Introduction History, Motivation, Whos & Whos15-May-11
  4. 4. What is Scala? General purpose, compiled language Relatively new (2003) Designed to express common programming patterns Concisely Elegantly In a type-safe way Originally intended to run on both the JVM and the CLR After some time, the .NET implementation fell aside Later on, thanks to a grant from Microsoft, work resumed Resembles dynamic languages such as Python / Ruby Combines OO and functional languages features15-May-11 WWW.TIKALK.COM
  5. 5. Personal Issues Created by Martin Odersky, a computer scientist at EPFL in Lausanne, Switzerland. Co-designer of Javas generics, and the original author of the current javac James Gosling: "If I needed to choose a language other than Java, it would be Scala" James Strachan (Groovy creator) has left Groovy and now advocates Scala "If I had known Scala when I created Groovy, I would have probably not created it at all "15-May-11 WWW.TIKALK.COM
  6. 6. Static TypingScala is a statically-typed language, unlike Ruby or Python Better IDE assistance Code completion Refactoring support Developer productivity enhanced Safer code via compile-time checks Better performance Enhancing developer productivity No need to wait for JSR-292s invokedynamic for performance boost As in Groovy, JRuby, Jython, … Code size is typically reduced by a factor of 2-3 compared to Java15-May-11 WWW.TIKALK.COM
  7. 7. Compatibility The Scala compiler generates Java bytecode 100% compatible with the JRE Seamless use Java objects in Scala and vice versa Deploy Scala applications to any JEE container / Android Reuse existing tools, servers, libraries Keep existing knowledge & workers Make the change gradually Java -> Jala -> Scala15-May-11 WWW.TIKALK.COM
  8. 8. What Went Wrong with Java? Oh, quite a few things: Java is over-verbose Stopped evolving Long adoption of new development idioms Functional programming, for instance Political / Marketing Issues (ok, Oracle) "Learning Java stunts your intellectual growth as a developer" [at least, some would say]15-May-11 WWW.TIKALK.COM
  9. 9. Why Prefer Scala Over Java ? Scala corrects many of Javas mistakes: Everything is an Object Type inference var dummy= Map[String,Int]() True Mixins, Traits, Duck Typing Functional programming language concepts Fresh start, no backward compatibility constraints Generics, anyone? More compact No getters, setters, equals(), hashCode(), toString(),...15-May-11 WWW.TIKALK.COM
  10. 10. ...Scala over Java Allows using new idioms and ways of programming Mostly just too laborious to do in Java Less LOC doing the same amount of work Better readability Never use anonymous inner classes again But still, leverage your Java code from Scala Productivity benefits Looks like a dynamically typed language While actually being 100% statically typed15-May-11 WWW.TIKALK.COM
  11. 11. Polyg-wha? It is common advice that you should learn multiple programming languages Scala integrates functional programming with OO programming Scala allows you to gradually learn functional techniques while still being able to use familiar object-oriented techniques Useful when migrating from Java Learning Scala may be easier than learning non- OO functional languages15-May-11 WWW.TIKALK.COM
  12. 12. Functional Concepts in Scala Functions are first-class values Convenient closure syntax Lazy evaluation Pattern matching Tailcall optimization Powerful generics (*) Still, Scala feels less functional because its core syntax is largely in the tradition of Java, not Lisp, ML, or Haskell, the three most prominent ancestors of functional language families)15-May-11 WWW.TIKALK.COM
  13. 13. Performance On par with Java Some features of Scala can produce faster code Specialization Inlining Laziness ...While other features makes it slower15-May-11 WWW.TIKALK.COM
  14. 14. Language Features Whats all the fuss about, then?15-May-11
  15. 15. Higher Order Functions Functions are first-class objects Can be assigned to variables Passed to other functions Same as any other data type Code made more concise and readable ...not quite as concise as in some other functional languages But much, much better than the equivalent Java code15-May-11 WWW.TIKALK.COM
  16. 16. Side Effects Problem In OO languages, objects may contain state - mutable instance data When a method uses mutable data, it now has side effects Reading or writing any state which is not an incoming or outgoing argument Functions with side effects Are harder to test Harder to reason about In general harder to get right As more side effects are added, they accumulate, making the composed function even more difficult to get right15-May-11 WWW.TIKALK.COM
  17. 17. Immutable Values Using immutable values... Makes it easier to write code without side effects Reduces the likelihood of concurrency bugs Can make code easier to read and understand Scala separates the concept of a val from a var: A val in Scala is like a final variable in Java A var is a regular variable No "default" mutable - forces you to think about that choice Scala also has support for immutable data structures When you add to immutable Map/List/Set in Scala, you get a new object: Old keys and values are not copied, but shared15-May-11 WWW.TIKALK.COM
  18. 18. Referential Transparency The Imperative Way: Use variables (mutable data) and loops The Functional Way: Recursion & higher order functions, without mutable data Referentially transparent -> Deterministic Calling a function with a specific set of values as arguments will always return exactly the same value15-May-11 WWW.TIKALK.COM
  19. 19. Actor Concurrency The Java thread/monitor model works quite well... For a small number of threads Dealing with a very small number of shared objects But a synchronized method isnt referentially transparent! Scala supports the Java approach, but it also provides another model for better scale-up: the Actor model Message-queue mechanism borrowed from Erlang A language designed for high concurrency15-May-11 WWW.TIKALK.COM
  20. 20. Actor Mechanics An Actor is responsible for maintaining data shared by multiple threads Only the Actor is allowed to access that data Threads communicate with the Actor by sending messages to it The Actor can respond by sending messages back If the other thread is an Actor Typically the messages are immutable Each Actor has a message inbox where incoming messages are queued Scalas Actor library handles the message transfers The programmer does not have to deal with synchronization15-May-11 WWW.TIKALK.COM
  21. 21. Actors Are Way Better High-load programs often suffer from: Data corruption due to concurrent access. Deadlock Resource bottleneck or starvation Javas monitors alleviates the first problem Scala Also takes care of the second15-May-11 WWW.TIKALK.COM
  22. 22. Actor Libraries There are 6-7 libraries in Java that implement the actor model (and 6 in Scala) Looking for a place to Start? Try Akka Scala has no benefit, other than being more inherently focused on distributed/parallel programming Version 2.9.0 has parallel collections, actors are built in Scala research group got a 2.3M Euro grant recently to develop a simpler programming model for parallel apps15-May-11 WWW.TIKALK.COM
  23. 23. Scala Ecosphere Make Or Break15-May-11
  24. 24. Companies that Use Scala There are more - WWW.TIKALK.COM
  25. 25. Scala Frameworks Akka: an alternative approach to JEE, with distribution support and several alternatives for concurrency models Lift: Full stack for web applications Scalate: Templating framework Play framework: Moving to Scala flockdb: a distributed fault tolerant graph database by twitter specs, scalatest, scalacheck: testing frameworks for scala that go beyong junit15-May-11 WWW.TIKALK.COM
  26. 26. Build / Development Environment sbt: A build tool that uses Scala for its build files and has great integration with various frameworks maven-scala-plugin: supports compilation, testing, documentation IDE Support: Eclipse, IntelliJ, NetBeans15-May-11 WWW.TIKALK.COM
  27. 27. Scala Job Trends15-May-11 WWW.TIKALK.COM
  28. 28. Q&A If you dont ask now - you can always ask later!15-May-11
  29. 29. Thank You zvika@tikalk.com15-May-11