Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA


Published on

Scala - This presentation by Luu Thanh Thuy CWI team from eXo Platform SEA

Published in: Technology
  • 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

Scala - By Luu Thanh Thuy CWI team from eXo Platform SEA

  1. 1. Scala a successor to java CWI team
  2. 2. Agenda <ul><ul><li>Pros and cons of Scala </li></ul></ul> Copyright 2011 eXo Platform <ul><ul><li>Introduction Scala language </li></ul></ul>
  3. 3. Scala a successor to java
  4. 4. Copyright 2011 eXo Platform What is Scala <ul><ul><li>- Scala is an object-oriented and functional language which is completely interoperable with Java </li></ul></ul><ul><ul><li>( the .NET version is currently under construction ) </li></ul></ul><ul><ul><li>- Scala is a program language created 2001 </li></ul></ul><ul><ul><li>- Scala runs on JVM </li></ul></ul><ul><ul><li>An open-source distribution of Scala has been available since Jan 2004 </li></ul></ul>
  5. 5. Copyright 2011 eXo Platform Problem with statically typed language <ul><ul><li>In principle, software should be constructed from re-usable parts (component) </li></ul></ul><ul><ul><li>In practice, software is still most often written “from scratch” </li></ul></ul><ul><ul><li>Programming languages share a part of the blame </li></ul></ul><ul><ul><li>Most existing languages offer only limited support for components. </li></ul></ul>
  6. 6. Copyright 2011 eXo Platform How to do better <ul><ul><li>Scalability can be achieved by unifying and generalizing functional and objected-oriented programming concept </li></ul></ul>
  7. 7. Copyright 2011 eXo Platform Why unifies FP and OOP <ul><ul><li>Scalability can be achieved by unifying and genenalizing functional and object -oriented programming concept. </li></ul></ul><ul><ul><li>Functioning language make it easy for building interesting thing from simple part using </li></ul></ul><ul><ul><ul><li>- Higher-order function </li></ul></ul></ul><ul><ul><ul><li>- Algebraic data type and pattern matching </li></ul></ul></ul><ul><ul><ul><li>-Parametric polimorphism </li></ul></ul></ul><ul><li>                     </li></ul><ul><li>Object-oriented language make it easy to adapt and extend complex system, using </li></ul><ul><li>- Subtyping and inheritance </li></ul><ul><li>-Dynamic configuration </li></ul><ul><li>-Classes and partial abstraction </li></ul>
  8. 8. Copyright 2011 eXo Platform 1 st 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.
  9. 9. Copyright 2011 eXo Platform 1 st Unification ADTs are class hierarchies With class hierarchies contents is encapsulaated in the object and accessed through methods With 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
  10. 10. Copyright 2011 eXo Platform 1 st 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
  11. 11. Copyright 2011 eXo Platform 2 nd Unification : functions are objects <ul><ul><li>Scala is functional language, in the sense that every function is a value </li></ul></ul><ul><ul><li>Scala is purely object-oriented. Since function is value, they must object, too </li></ul></ul><ul><ul><li>Functions can be anonymous, curried, nested </li></ul></ul><ul><ul><li>A function is instance of class Function0 or Function1 or... </li></ul></ul><ul><ul><li>There exist one function class for all number of parameters. </li></ul></ul><ul><ul><li>A class implementing FunctionX must define an apply method with the correct number of parameters </li></ul></ul><ul><ul><li>object addOne extends Function1[Int, Int] { </li></ul></ul><ul><ul><li>Def apply(num:Int) :Int= num + 1 </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>AddOne(23) will return 24 </li></ul></ul>
  12. 12. Copyright 2011 eXo Platform The type of function <ul><ul><li>A shorthand syntax for writing the type of function also exists </li></ul></ul><ul><ul><li>Function0[Int] become()=>Int </li></ul></ul><ul><ul><li>Function1[String,Person] become String=>Person </li></ul></ul><ul><ul><li>Function2[Int,Int,Int] becomes (Int, Int) =>Int </li></ul></ul><ul><ul><li>Int=>Int=>Int is Function1[Int, Int] </li></ul></ul>
  13. 13. Copyright 2011 eXo Platform Taking advantage of function <ul><ul><li>A functional programming language style offers real benefits for modular programs </li></ul></ul><ul><ul><li>A module can be parameterized by function, not only by state </li></ul></ul><ul><ul><li>Functions can be passed from modules to module </li></ul></ul><ul><ul><li>Scala's function-as-objects allow an easy integration of functions in a traditional OO environment </li></ul></ul>
  14. 14. Copyright 2011 eXo Platform Functional programming style <ul><ul><li>Writing in functional style can be difficult for seasoned OO programmers </li></ul></ul><ul><ul><li>Behaviour is no longer attached to an object but moves freely </li></ul></ul><ul><ul><li>State become less important : there are no methods depending on it </li></ul></ul><ul><ul><li>Immutable objects become natural : why deal with state when a function can simply return a new object </li></ul></ul><ul><ul><li>In other words, use state sparingly in Scala , functions and immutable object help structure messy code </li></ul></ul>
  15. 15. Copyright 2011 eXo Platform Modular system with single inheritance <ul><ul><li>In single class inheritance languages </li></ul></ul><ul><ul><ul><li>Point merging behaviours of different classes is tricky </li></ul></ul></ul><ul><ul><ul><li>Adaptor code is required </li></ul></ul></ul><ul><ul><ul><li>Which make the ralation brittle </li></ul></ul></ul><ul><ul><ul><li>Often, module reengineering is required </li></ul></ul></ul><ul><ul><ul><li>Java's interfaces provides some help , but are clearly insufficient </li></ul></ul></ul>
  16. 16. Copyright 2011 eXo Platform 3 rd Unification: modules are objects <ul><ul><li>Scala has a clean and powerful type system which enables new ways of abstracting and composing components. </li></ul></ul><ul><ul><li>A component is a program part, to be combined with other parts in larger applications. </li></ul></ul><ul><ul><li>Requirement : Components should be reusable. </li></ul></ul><ul><ul><li>To be reusable in new contexts, a component needs interfaces describing its provided as well its required services </li></ul></ul><ul><ul><li>Most current components are not very resusable. </li></ul></ul><ul><ul><li>Most current languages can specify only provided services, not required services </li></ul></ul><ul><ul><li>Note : Component != API </li></ul></ul>
  17. 17. Copyright 2011 eXo Platform Components in Scala <ul><ul><li>In Scala </li></ul></ul><ul><ul><li>Component = Class </li></ul></ul><ul><ul><li>Interface = Abstract Class </li></ul></ul><ul><ul><li>Required Component = Abstract Type member or </li></ul></ul><ul><ul><li>explicit self-Type </li></ul></ul><ul><ul><li>Composition = Modular Mixin Composition </li></ul></ul><ul><ul><li>Advantages: </li></ul></ul><ul><ul><li>Components instantiate to object, which are first-class values </li></ul></ul><ul><ul><li>Recursive references between components are supported </li></ul></ul><ul><ul><li>Inheritance with overriding is supported </li></ul></ul><ul><ul><li>Sub-components are identified by name </li></ul></ul><ul><ul><li>=> No explicit “wiring” is need </li></ul></ul>
  18. 18. Copyright 2011 eXo Platform Mixins <ul><ul><li>Full multiple inheritance is often too complex to be of great use </li></ul></ul><ul><ul><li>Scala provides mixins as compromise </li></ul></ul><ul><ul><li>A class can inherit from multiple traits </li></ul></ul><ul><ul><li>A trait is a special kind of class with implements some behaviour </li></ul></ul><ul><ul><li>There must be a common parant class with the inherited mixin </li></ul></ul>
  19. 19. Copyright 2011 eXo Platform Traits <ul><ul><li>A trait is defined like a class, but using the trait keyword instead </li></ul></ul><ul><ul><li>trait Flying extends Vehicles { </li></ul></ul><ul><ul><li>def takeOff = …/ concrete </li></ul></ul><ul><ul><li>def land:Unit //abstract </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>All members inherited from Vehicle can be used </li></ul></ul><ul><ul><li>this trait will eventually be mixed-in with a class extending Vehicle </li></ul></ul>
  20. 20. Copyright 2011 eXo Platform Inheriting trait <ul><ul><li>A trait can be inherited </li></ul></ul><ul><ul><li>When defining a class </li></ul></ul><ul><ul><li>class JamesBondCar extends Car with Flying with Diving </li></ul></ul><ul><ul><li>Or when creating an instance </li></ul></ul><ul><ul><li>val jbsCar = new Car with Flying with Diving </li></ul></ul><ul><ul><li>When a class only extends mixins, it will automatically also extends AnyRef </li></ul></ul>
  21. 21. Copyright 2011 eXo Platform Requiring a behaviour <ul><ul><li>When multiple traits are inherited </li></ul></ul><ul><ul><li>They can refer to members of their common super car </li></ul></ul><ul><ul><li>But not to members of other mixed-in traits </li></ul></ul><ul><ul><li>A trait can required other class or trait </li></ul></ul><ul><ul><li>it can only be mixed-in when the requirement is available. </li></ul></ul><ul><ul><li>trait Reading extends Person requires Seeing </li></ul></ul>
  22. 22. Copyright 2011 eXo Platform Concurrent programming <ul><ul><li>Scala's primary concurrency construct is actors </li></ul></ul><ul><ul><li>Actor are basically concurrent processes that communicate by exchanging message </li></ul></ul><ul><ul><li>Scala actor library provides asynchronous message sends </li></ul></ul>
  23. 23. Q & A
  24. 24. Thank you!