Presentation is loading. Please wait.

Presentation is loading. Please wait.

Case Classes in Scala Intro

Similar presentations


Presentation on theme: "Case Classes in Scala Intro"— Presentation transcript:

1 Case Classes in Scala Intro
UMBC CMSC 331

2 Basic Properties Case classes are regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via pattern matching.

3 Example abstract class Term case class Var(name: String) extends Term
case class Fun(arg: String, body: Term) extends Term case class App(f: Term, v: Term) extends Term

4 Code Code Example Fun("x", Fun("y", App(Var("x"), Var("y"))))
For every case class the Scala compiler generates equals method which implements structural equality and atoString method. For instance and so ... Fun("x", Fun("y", App(Var("x"), Var("y"))))

5 Results val x1 = Var("x") val x2 = Var("x") val y1 = Var("y")
println("" + x1 + " == " + x2 + " => " + (x1 == x2)) println("" + x1 + " == " + y1 + " => " + (x1 == y1)) Var(“x”) == Var(“x”) =>true Var(“x”) == Var(“y”) => false

6 Another Example abstract class IntTree
case class Empty() extends IntTree case class Node(value: Int, left: IntTree, right:IntTree) extends IntTree

7 References A tour of Scala : Traits (see http://www.scala-lang.org)
Programming in Scala, Martin Odersky et al, Artima press 2009 Beginning Scala, David Pollak, Apress, 2009


Download ppt "Case Classes in Scala Intro"

Similar presentations


Ads by Google