Upload
zvikapika
View
2.976
Download
0
Tags:
Embed Size (px)
Citation preview
15-May-11
By : Zvika Markfeld Tikal Knowledge
Scala TCE
Introduction to The Scala Language
Developer Productivity Redefined
WWW.TIKALK.COM15-May-11
Agenda
IntroductionLanguage FeaturesEcosphereQ & A
15-May-11
IntroductionHistory, Motivation, Whos & Whos
WWW.TIKALK.COM15-May-11
What is Scala?
General purpose, compiled languageRelatively new (2003)Designed to express common programming patterns
ConciselyElegantlyIn a type-safe way
Originally intended to run on both the JVM and the CLRAfter some time, the .NET implementation fell asideLater on, thanks to a grant from Microsoft, work resumed
Resembles dynamic languages such as Python / RubyCombines OO and functional languages features
WWW.TIKALK.COM15-May-11
Personal Issues
Created by Martin Odersky, a computer scientist at EPFL in Lausanne, Switzerland.
Co-designer of Java's 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 "
Scala is a statically-typed language, unlike Ruby or PythonBetter IDE assistance
Code completionRefactoring supportDeveloper productivity enhanced
Safer code via compile-time checksBetter performanceEnhancing developer productivityNo need to wait for JSR-292's invokedynamic for performance boost
As in Groovy, JRuby, Jython, …Code size is typically reduced by a factor of 2-3 compared to Java
WWW.TIKALK.COM15-May-11
Static Typing
The Scala compiler generates Java bytecode 100% compatible with the JRESeamless use Java objects in Scala and vice versaDeploy Scala applications to any JEE container / AndroidReuse existing tools, servers, librariesKeep existing knowledge & workersMake the change gradually
Java -> Jala -> Scala
WWW.TIKALK.COM15-May-11
Compatibility
WWW.TIKALK.COM15-May-11
What Went Wrong with Java?
Oh, quite a few things:
Java is over-verboseStopped evolvingLong 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]
WWW.TIKALK.COM15-May-11
Why Prefer Scala Over Java ?
Scala corrects many of Java's mistakes:
Everything is an ObjectType inference
var dummy= Map[String,Int]()True Mixins, Traits, Duck TypingFunctional programming language conceptsFresh start, no backward compatibility constraints
Generics, anyone?More compact
No getters, setters, equals(), hashCode(), toString(),...
Allows using new idioms and ways of programmingMostly just too laborious to do in Java
Less LOC doing the same amount of workBetter readability
Never use anonymous inner classes againBut still, leverage your Java code from ScalaProductivity benefits
Looks like a dynamically typed languageWhile actually being 100% statically typed
WWW.TIKALK.COM15-May-11
...Scala over Java
WWW.TIKALK.COM15-May-11
Polyg-wha?
It is common advice that you should learn multiple programming languagesScala integrates functional programming with OO programmingScala allows you to gradually learn functional techniques while still being able to use familiar object-oriented techniques
Useful when migrating from JavaLearning Scala may be easier than learning non-OO functional languages
WWW.TIKALK.COM15-May-11
Functional Concepts in Scala
Functions are first-class valuesConvenient closure syntaxLazy evaluationPattern matchingTailcall optimizationPowerful 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)
WWW.TIKALK.COM15-May-11
Performance
On par with JavaSome features of Scala can produce faster code
SpecializationInliningLaziness
...While other features makes it slower
15-May-11
Language FeaturesWhat's all the fuss about, then?
WWW.TIKALK.COM15-May-11
Higher Order Functions
Functions are first-class objectsCan be assigned to variablesPassed to other functionsSame as any other data type
Code made more concise and readable...not quite as concise as in some other functional languagesBut much, much better than the equivalent Java code
WWW.TIKALK.COM15-May-11
Side Effects Problem
In OO languages, objects may contain state - mutable instance dataWhen 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 effectsAre harder to testHarder to reason aboutIn general harder to get right
As more side effects are added, they accumulate, making the composed function even more difficult to get right
WWW.TIKALK.COM15-May-11
Immutable Values
Using immutable values...Makes it easier to write code without side effectsReduces 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 JavaA var is a regular variableNo "default" mutable - forces you to think about that choice
Scala also has support for immutable data structuresWhen you add to immutable Map/List/Set in Scala, you get a new object: Old keys and values are not copied, but shared
WWW.TIKALK.COM15-May-11
Referential Transparency
The Imperative Way: Use variables (mutable data) and loops
The Functional Way:Recursion & higher order functions, without mutable data
Referentially transparent -> DeterministicCalling a function with a specific set of values as arguments will always return exactly the same value
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 isn't 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 ErlangA language designed for high concurrency
WWW.TIKALK.COM15-May-11
Actor Concurrency
WWW.TIKALK.COM15-May-11
Actor Mechanics
An Actor is responsible for maintaining data shared by multiple threadsOnly the Actor is allowed to access that dataThreads communicate with the Actor by sending messages to itThe Actor can respond by sending messages back
If the other thread is an ActorTypically the messages are immutableEach Actor has a message inbox where incoming messages are queuedScala's Actor library handles the message transfers
The programmer does not have to deal with synchronization
WWW.TIKALK.COM15-May-11
Actors Are Way Better
High-load programs often suffer from:Data corruption due to concurrent access.DeadlockResource bottleneck or starvation
Java's monitors alleviates the first problemScala Also takes care of the second
WWW.TIKALK.COM15-May-11
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 AkkaScala has no benefit, other than being more inherently focused on distributed/parallel programming
Version 2.9.0 has parallel collections, actors are built inScala research group got a 2.3M Euro grant recently to develop a simpler programming model for parallel apps
15-May-11
Scala EcosphereMake Or Break
WWW.TIKALK.COM15-May-11
Companies that Use Scala
There are more - http://www.scala-lang.org/node/1658
WWW.TIKALK.COM15-May-11
Scala Frameworks
Akka:an alternative approach to JEE, with distribution support and several alternatives for concurrency modelsLift:Full stack for web applicationsScalate:Templating frameworkPlay framework: Moving to Scalaflockdb:a distributed fault tolerant graph database by twitterspecs, scalatest, scalacheck:testing frameworks for scala that go beyong junit
WWW.TIKALK.COM15-May-11
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, NetBeans
WWW.TIKALK.COM15-May-11
Scala Job Trends
15-May-11
Q & AIf you don't ask now -
you can always ask later!