SlideShare a Scribd company logo
The Scala Programming
Language
presented by Donna Malayeri
Why a new language?
 Goal was to create a language with better support
for component software
 Two hypotheses:
 Programming language for component software
should be scalable
 The same concepts describe small and large parts
 Rather than adding lots of primitives, focus is on
abstraction, composition, and decomposition
 Language that unifies OOP and functional
programming can provide scalable support for
components
 Adoption is key for testing this hypothesis
 Scala interoperates with Java and .NET
Features of Scala
 Scala is both functional and object-oriented
 every value is an object
 every function is a value--including methods
 Scala is statically typed
 includes a local type inference system:
in Java 1.5:
Pair p = new Pair<Integer, String>(1, "Scala");
in Scala:
val p = new MyPair(1, "scala");
More features
 Supports lightweight syntax for anonymous
functions, higher-order functions, nested
functions, currying
 ML-style pattern matching
 Integration with XML
 can write XML directly in Scala program
 can convert XML DTD into Scala class
definitions
 Support for regular expression patterns
Other features
 Allows defining new control structures
without using macros, and while
maintaining static typing
 Any function can be used as an infix or
postfix operator
 Can define methods named +, <= or ::
Automatic Closure Construction
 Allows programmers to make their own
control structures
 Can tag the parameters of methods with the
modifier def.
 When method is called, the actual def
parameters are not evaluated and a no-
argument function is passed
While loop example
object TargetTest1 with Application {
def loopWhile(def cond: Boolean)(def body: Unit): Unit =
if (cond) {
body;
loopWhile(cond)(body);
}
var i = 10;
loopWhile (i > 0) {
Console.println(i);
i = i - 1
}
}
Define loopWhile method
Use it with nice syntax
Scala class hierarchy
Scala object system
 Class-based
 Single inheritance
 Can define singleton objects easily
 Subtyping is nominal
 Traits, compound types, and views allow for
more flexibility
Classes and Objects
trait Nat;
object Zero extends Nat {
def isZero: boolean = true;
def pred: Nat =
throw new Error("Zero.pred");
}
class Succ(n: Nat) extends Nat {
def isZero: boolean = false;
def pred: Nat = n;
}
Traits
 Similar to interfaces in Java
 They may have implementations of methods
 But can’t contain state
 Can be multiply inherited from
Example of traits
trait Similarity {
def isSimilar(x: Any): Boolean;
def isNotSimilar(x: Any): Boolean = !isSimilar(x);
}
class Point(xc: Int, yc: Int) with Similarity {
var x: Int = xc;
var y: Int = yc;
def isSimilar(obj: Any) =
obj.isInstanceOf[Point] &&
obj.asInstanceOf[Point].x == x;
}
Mixin class composition
 Basic inheritance model is single inheritance
 But mixin classes allow more flexibility
class Point2D(xc: Int, yc: Int) {
val x = xc;
val y = yc;
// methods for manipulating Point2Ds
}
class ColoredPoint2D(u: Int, v: Int, c: String)
extends Point2D(u, v) {
var color = c;
def setColor(newCol: String): Unit = color = newCol;
}
Mixin class composition example
ColoredPoint2D
Point2D
class Point3D(xc: Int, yc: Int, zc: Int)
extends Point2D(xc, yc) {
val z = zc;
// code for manipulating Point3Ds
}
Point3D
class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String)
extends Point3D(xc, yc, zc)
with ColoredPoint2D(xc, yc, col);
ColoredPoint3D
ColoredPoint2D
Mixin class composition
 Mixin composition adds members explicitly
defined in ColoredPoint2D
(members that weren’t inherited)
 Mixing a class C into another class D is legal
only as long as D’s superclass is a subclass of
C’s superclass.
 i.e., D must inherit at least everything that C
inherited
 Why?
Mixin class composition
 Remember that only members explicitly
defined in ColoredPoint2D are mixin
inherited
 So, if those members refer to definitions
that were inherited from Point2D, they had
better exist in ColoredPoint3D
 They do, since
ColoredPoint3D extends Point3D
which extends Point2D
Views
 Defines a coercion from one type to another
 Similar to conversion operators in C++/C#
trait Set {
def include(x: int): Set;
def contains(x: int): boolean
}
def view(list: List) : Set = new Set {
def include(x: int): Set = x prepend xs;
def contains(x: int): boolean =
!isEmpty &&
(list.head == x || list.tail contains x)
}
Views
 Views are inserted automatically by the Scala
compiler
 If e is of type T then a view is applied to e if:
 expected type of e is not T (or a supertype)
 a member selected from e is not a member of T
 Compiler uses only views in scope
Suppose xs : List and view above is in scope
val s: Set = xs;
xs contains x
val s: Set = view(xs);
view(xs) contains x
Compound types motivation
def cloneAndReset(obj: ?): Cloneable = {
val cloned = obj.clone();
obj.reset;
cloned
}
trait Resetable {
def reset: Unit;
}
trait Cloneable {
def clone();
}
Compound types
 In Java, the “solution” is:
interface CloneableAndResetable extends
Cloneable, Resetable
 But if the original object did not use the
CloneableAndResetable interface, it won’t
work
 Scala solution: use compound types (also
called intersection types)
def cloneAndReset(obj: Cloneable with Resetable):
Cloneable = {
...
}
Variance annotations
class Array[a] {
def get(index: int): a
def set(index: int, elem: a): unit;
}
 Array[String] is not a subtype of Array[Any]
 If it were, we could do this:
val x = new Array[String](1);
val y : Array[Any] = x;
y.set(0, new FooBar());
// just stored a FooBar in a String array!
Variance Annotations
 Covariance is ok with functional data structures
trait GenList[+T] {
def isEmpty: boolean;
def head: T;
def tail: GenList[T]
}
object Empty extends GenList[All] {
def isEmpty: boolean = true;
def head: All = throw new Error("Empty.head");
def tail: List[All] = throw new Error("Empty.tail");
}
class Cons[+T](x: T, xs: GenList[T]) extends GenList[T] {
def isEmpty: boolean = false;
def head: T = x;
def tail: GenList[T] = xs
}
Variance Annotations
 Can also have contravariant type parameters
 Useful for an object that can only be written to
 Scala checks that variance annotations are
sound
 covariant positions: immutable field types,
method results
 contravariant: method argument types
 Type system ensures that covariant
parameters are only used covariant positions
(similar for contravariant)
Types as members
abstract class AbsCell {
type T;
val init: T;
private var value: T = init;
def get: T = value;
def set(x: T): unit = { value = x }
}
def createCell : AbsCell {
new AbsCell { type T = int; val init = 1 }
}
 Clients of createCell cannot rely on the fact that
T is int, since this information is hidden from them
Discussion/Critiques
 Scala has nominal subtyping. Is this good?
 Inheritance and subtyping are conflated in
Scala. Shouldn’t they be separated?
 Mixins in MzScheme vs. Scala – MzScheme
allows a class to parameterize its supertype,
while Scala does not
 Type system does not distinguish null
references from non-null references

More Related Content

Similar to scala.ppt

Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
manaswinimysore
 
Programming in Scala - Lecture Three
Programming in Scala - Lecture ThreeProgramming in Scala - Lecture Three
Programming in Scala - Lecture Three
Angelo Corsaro
 
TI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type ParameterizationTI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type ParameterizationEelco Visser
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
Michael Stal
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
Meetu Maltiar
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
Xebia IT Architects
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
league
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
Michal Bigos
 
Scala idioms
Scala idiomsScala idioms
Scala idioms
Knoldus Inc.
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian DragosScala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian Dragos
3Pillar Global
 
Introductiontoprogramminginscala
IntroductiontoprogramminginscalaIntroductiontoprogramminginscala
Introductiontoprogramminginscala
Amuhinda Hungai
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
Miles Sabin
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersSkills Matter
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
Venkateswaran Kandasamy
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
Arduino Aficionado
 
Functions & closures
Functions & closuresFunctions & closures
Functions & closures
Knoldus Inc.
 
Functions & Closures in Scala
Functions & Closures in ScalaFunctions & Closures in Scala
Functions & Closures in Scala
Knoldus Inc.
 
Functions & Closures in Scala
Functions & Closures in ScalaFunctions & Closures in Scala
Functions & Closures in Scala
Neelkanth Sachdeva
 

Similar to scala.ppt (20)

Intro to Scala
 Intro to Scala Intro to Scala
Intro to Scala
 
Programming in Scala - Lecture Three
Programming in Scala - Lecture ThreeProgramming in Scala - Lecture Three
Programming in Scala - Lecture Three
 
TI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type ParameterizationTI1220 Lecture 8: Traits & Type Parameterization
TI1220 Lecture 8: Traits & Type Parameterization
 
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scalaQcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_scala
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Getting Started With Scala
Getting Started With ScalaGetting Started With Scala
Getting Started With Scala
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
 
Scala idioms
Scala idiomsScala idioms
Scala idioms
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian DragosScala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian Dragos
 
Introductiontoprogramminginscala
IntroductiontoprogramminginscalaIntroductiontoprogramminginscala
Introductiontoprogramminginscala
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Scala cheatsheet
Scala cheatsheetScala cheatsheet
Scala cheatsheet
 
Functional object
Functional objectFunctional object
Functional object
 
Functions & closures
Functions & closuresFunctions & closures
Functions & closures
 
Functions & Closures in Scala
Functions & Closures in ScalaFunctions & Closures in Scala
Functions & Closures in Scala
 
Functions & Closures in Scala
Functions & Closures in ScalaFunctions & Closures in Scala
Functions & Closures in Scala
 

Recently uploaded

Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 

Recently uploaded (20)

Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

scala.ppt

  • 2. Why a new language?  Goal was to create a language with better support for component software  Two hypotheses:  Programming language for component software should be scalable  The same concepts describe small and large parts  Rather than adding lots of primitives, focus is on abstraction, composition, and decomposition  Language that unifies OOP and functional programming can provide scalable support for components  Adoption is key for testing this hypothesis  Scala interoperates with Java and .NET
  • 3. Features of Scala  Scala is both functional and object-oriented  every value is an object  every function is a value--including methods  Scala is statically typed  includes a local type inference system: in Java 1.5: Pair p = new Pair<Integer, String>(1, "Scala"); in Scala: val p = new MyPair(1, "scala");
  • 4. More features  Supports lightweight syntax for anonymous functions, higher-order functions, nested functions, currying  ML-style pattern matching  Integration with XML  can write XML directly in Scala program  can convert XML DTD into Scala class definitions  Support for regular expression patterns
  • 5. Other features  Allows defining new control structures without using macros, and while maintaining static typing  Any function can be used as an infix or postfix operator  Can define methods named +, <= or ::
  • 6. Automatic Closure Construction  Allows programmers to make their own control structures  Can tag the parameters of methods with the modifier def.  When method is called, the actual def parameters are not evaluated and a no- argument function is passed
  • 7. While loop example object TargetTest1 with Application { def loopWhile(def cond: Boolean)(def body: Unit): Unit = if (cond) { body; loopWhile(cond)(body); } var i = 10; loopWhile (i > 0) { Console.println(i); i = i - 1 } } Define loopWhile method Use it with nice syntax
  • 9. Scala object system  Class-based  Single inheritance  Can define singleton objects easily  Subtyping is nominal  Traits, compound types, and views allow for more flexibility
  • 10. Classes and Objects trait Nat; object Zero extends Nat { def isZero: boolean = true; def pred: Nat = throw new Error("Zero.pred"); } class Succ(n: Nat) extends Nat { def isZero: boolean = false; def pred: Nat = n; }
  • 11. Traits  Similar to interfaces in Java  They may have implementations of methods  But can’t contain state  Can be multiply inherited from
  • 12. Example of traits trait Similarity { def isSimilar(x: Any): Boolean; def isNotSimilar(x: Any): Boolean = !isSimilar(x); } class Point(xc: Int, yc: Int) with Similarity { var x: Int = xc; var y: Int = yc; def isSimilar(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == x; }
  • 13. Mixin class composition  Basic inheritance model is single inheritance  But mixin classes allow more flexibility class Point2D(xc: Int, yc: Int) { val x = xc; val y = yc; // methods for manipulating Point2Ds } class ColoredPoint2D(u: Int, v: Int, c: String) extends Point2D(u, v) { var color = c; def setColor(newCol: String): Unit = color = newCol; }
  • 14. Mixin class composition example ColoredPoint2D Point2D class Point3D(xc: Int, yc: Int, zc: Int) extends Point2D(xc, yc) { val z = zc; // code for manipulating Point3Ds } Point3D class ColoredPoint3D(xc: Int, yc: Int, zc: Int, col: String) extends Point3D(xc, yc, zc) with ColoredPoint2D(xc, yc, col); ColoredPoint3D ColoredPoint2D
  • 15. Mixin class composition  Mixin composition adds members explicitly defined in ColoredPoint2D (members that weren’t inherited)  Mixing a class C into another class D is legal only as long as D’s superclass is a subclass of C’s superclass.  i.e., D must inherit at least everything that C inherited  Why?
  • 16. Mixin class composition  Remember that only members explicitly defined in ColoredPoint2D are mixin inherited  So, if those members refer to definitions that were inherited from Point2D, they had better exist in ColoredPoint3D  They do, since ColoredPoint3D extends Point3D which extends Point2D
  • 17. Views  Defines a coercion from one type to another  Similar to conversion operators in C++/C# trait Set { def include(x: int): Set; def contains(x: int): boolean } def view(list: List) : Set = new Set { def include(x: int): Set = x prepend xs; def contains(x: int): boolean = !isEmpty && (list.head == x || list.tail contains x) }
  • 18. Views  Views are inserted automatically by the Scala compiler  If e is of type T then a view is applied to e if:  expected type of e is not T (or a supertype)  a member selected from e is not a member of T  Compiler uses only views in scope Suppose xs : List and view above is in scope val s: Set = xs; xs contains x val s: Set = view(xs); view(xs) contains x
  • 19. Compound types motivation def cloneAndReset(obj: ?): Cloneable = { val cloned = obj.clone(); obj.reset; cloned } trait Resetable { def reset: Unit; } trait Cloneable { def clone(); }
  • 20. Compound types  In Java, the “solution” is: interface CloneableAndResetable extends Cloneable, Resetable  But if the original object did not use the CloneableAndResetable interface, it won’t work  Scala solution: use compound types (also called intersection types) def cloneAndReset(obj: Cloneable with Resetable): Cloneable = { ... }
  • 21. Variance annotations class Array[a] { def get(index: int): a def set(index: int, elem: a): unit; }  Array[String] is not a subtype of Array[Any]  If it were, we could do this: val x = new Array[String](1); val y : Array[Any] = x; y.set(0, new FooBar()); // just stored a FooBar in a String array!
  • 22. Variance Annotations  Covariance is ok with functional data structures trait GenList[+T] { def isEmpty: boolean; def head: T; def tail: GenList[T] } object Empty extends GenList[All] { def isEmpty: boolean = true; def head: All = throw new Error("Empty.head"); def tail: List[All] = throw new Error("Empty.tail"); } class Cons[+T](x: T, xs: GenList[T]) extends GenList[T] { def isEmpty: boolean = false; def head: T = x; def tail: GenList[T] = xs }
  • 23. Variance Annotations  Can also have contravariant type parameters  Useful for an object that can only be written to  Scala checks that variance annotations are sound  covariant positions: immutable field types, method results  contravariant: method argument types  Type system ensures that covariant parameters are only used covariant positions (similar for contravariant)
  • 24. Types as members abstract class AbsCell { type T; val init: T; private var value: T = init; def get: T = value; def set(x: T): unit = { value = x } } def createCell : AbsCell { new AbsCell { type T = int; val init = 1 } }  Clients of createCell cannot rely on the fact that T is int, since this information is hidden from them
  • 25. Discussion/Critiques  Scala has nominal subtyping. Is this good?  Inheritance and subtyping are conflated in Scala. Shouldn’t they be separated?  Mixins in MzScheme vs. Scala – MzScheme allows a class to parameterize its supertype, while Scala does not  Type system does not distinguish null references from non-null references