scala - by luu thanh thuy cwi team from exo platform sea
DESCRIPTION
Scala - This presentation by Luu Thanh Thuy CWI team from eXo Platform SEATRANSCRIPT
![Page 1: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/1.jpg)
Scala a successor to java
CWI team
![Page 2: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/2.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Agenda
• Pros and cons of Scala
• Introduction Scala language
![Page 3: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/3.jpg)
Scala a successor to java
![Page 4: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/4.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
What is Scala
- Scala is an object-oriented and functional language which is completely interoperable with Java
(the .NET version is currently under construction)- Scala is a program language created 2001 - Scala runs on JVM
An open-source distribution of Scala has been available since Jan 2004
![Page 5: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/5.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Problem with statically typed language
• In principle, software should be constructed from re-usable parts (component)
• In practice, software is still most often written “from scratch”
• Programming languages share a part of the blame• Most existing languages offer only limited support for
components.
![Page 6: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/6.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
How to do better
• Scalability can be achieved by unifying and generalizing functional and objected-oriented programming concept
![Page 7: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/7.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Why unifies FP and OOP
Scalability can be achieved by unifying and genenalizing functional and object -oriented programming concept.
Functioning language make it easy for building interesting thing from simple part using- Higher-order function- Algebraic data type and pattern matching-Parametric polimorphism
Object-oriented language make it easy to adapt and extend complex system, using - Subtyping and inheritance -Dynamic configuration -Classes and partial abstraction
![Page 8: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/8.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
1st Unification ADTs are class hierarchies
Many functional languages have algebraic data types and pattern matching =>Concise and canonical manipulation of data structure
From object-oriented point of view -”ADTs are not extensible” - “ADTs violate the purity of the OO data model” - “Pattern matching breaks encapsulation” => using class hierarchies.
![Page 9: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/9.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
1st Unification ADTs are class hierarchies
With class hierarchies contents is encapsulaated in the object and accessed through methodsWith ADTs : data is accessed through decomposing the value by pattern matching
ADTs and class hierarchies have complementary strength and weakness - ADTs allow easy extension of operations supported by the data- While class hierarchies allow easy addition of data variants
![Page 10: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/10.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
1st Unification ADTs are class hierarchies
ADTs can be encoded using case classes- Case classes are like normal classes.- Instance constructors can be recovered by pattern matching- Structural equality is used for comparison- The new keyword is optional for instance creation.Case class ClockTime(hour: Int, min:Int) is a valid case class definition. ClockTime(10,30) creates an instance
![Page 11: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/11.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
2nd Unification : functions are objects
• Scala is functional language, in the sense that every function is a value• Scala is purely object-oriented. Since function is value, they must
object, too• Functions can be anonymous, curried, nested• A function is instance of class Function0 or Function1 or...• There exist one function class for all number of parameters.• A class implementing FunctionX must define an apply method with the
correct number of parameters
object addOne extends Function1[Int, Int] { Def apply(num:Int) :Int= num + 1}AddOne(23) will return 24
![Page 12: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/12.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
The type of function
• A shorthand syntax for writing the type of function also exists
• Function0[Int] become()=>Int
• Function1[String,Person] become String=>Person
• Function2[Int,Int,Int] becomes (Int, Int) =>Int
• Int=>Int=>Int is Function1[Int, Int]
![Page 13: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/13.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Taking advantage of function
A functional programming language style offers real benefits for modular programs
• A module can be parameterized by function, not only by state
• Functions can be passed from modules to moduleScala's function-as-objects allow an easy integration
of functions in a traditional OO environment
![Page 14: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/14.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Functional programming style
Writing in functional style can be difficult for seasoned OO programmers
• Behaviour is no longer attached to an object but moves freely• State become less important : there are no methods
depending on it• Immutable objects become natural : why deal with state when
a function can simply return a new objectIn other words, use state sparingly in Scala , functions and
immutable object help structure messy code
![Page 15: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/15.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Modular system with single inheritance
• In single class inheritance languageso Point merging behaviours of different classes is trickyo Adaptor code is requiredo Which make the ralation brittleOften, module reengineering is requiredJava's interfaces provides some help , but are clearly
insufficient
![Page 16: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/16.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
3rd Unification: modules are objects
• Scala has a clean and powerful type system which enables new ways of abstracting and composing components.
A component is a program part, to be combined with other parts in larger applications.
Requirement : Components should be reusable.To be reusable in new contexts, a component needs
interfaces describing its provided as well its required services
• Most current components are not very resusable.• Most current languages can specify only provided
services, not required servicesNote : Component != API
![Page 17: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/17.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Components in Scala
In ScalaComponent = ClassInterface = Abstract ClassRequired Component = Abstract Type member or explicit self-TypeComposition = Modular Mixin CompositionAdvantages:• Components instantiate to object, which are first-class
values• Recursive references between components are supported• Inheritance with overriding is supported• Sub-components are identified by name=> No explicit “wiring” is need
![Page 18: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/18.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Mixins
• Full multiple inheritance is often too complex to be of great use• Scala provides mixins as compromise• A class can inherit from multiple traits• A trait is a special kind of class with implements some
behaviour• There must be a common parant class with the inherited mixin
![Page 19: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/19.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Traits
A trait is defined like a class, but using the trait keyword instead
trait Flying extends Vehicles { def takeOff = …/ concrete def land:Unit //abstract}
All members inherited from Vehicle can be usedthis trait will eventually be mixed-in with a class extending Vehicle
![Page 20: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/20.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Inheriting trait
A trait can be inherited• When defining a classclass JamesBondCar extends Car with Flying with Diving• Or when creating an instanceval jbsCar = new Car with Flying with DivingWhen a class only extends mixins, it will automatically also extends
AnyRef
![Page 21: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/21.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Requiring a behaviour
When multiple traits are inherited
• They can refer to members of their common super car
• But not to members of other mixed-in traitsA trait can required other class or trait
it can only be mixed-in when the requirement is available.
trait Reading extends Person requires Seeing
![Page 22: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/22.jpg)
www.exoplatform.comCopyright 2011 eXo Platform
Concurrent programming
Scala's primary concurrency construct is actors• Actor are basically concurrent processes that
communicate by exchanging message• Scala actor library provides asynchronous message
sends
![Page 23: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/23.jpg)
Q & A
![Page 24: Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA](https://reader033.vdocuments.net/reader033/viewer/2022061211/54922408ac795944288b46d1/html5/thumbnails/24.jpg)
Thank you!