4.6 Pattern Matching.pdf - Pattern Matching Reminder...

  • No School
  • AA 1
  • 15

This preview shows page 1 - 5 out of 15 pages.

Pattern Matching
Image of page 1
Reminder: Decomposition The task we are trying to solve is find a general and convenient way to access objects in a extensible class hierarchy. Attempts seen previously : Classification and access methods : quadratic explosion Type tests and casts : unsafe, low-level Object-oriented decomposition : does not always work, need to touch all classes to add a new method.
Image of page 2
Solution 2: Functional Decomposition with Pattern Matching Observation: the sole purpose of test and accessor functions is to reverse the construction process: Which subclass was used? What were the arguments of the constructor? This situation is so common that many functional languages, Scala included, automate it.
Image of page 3
Case Classes A case class definition is similar to a normal class definition, except that it is preceded by the modifier case . For example: trait Expr case class Number(n : Int ) extends Expr case class Sum(e1 : Expr , e2 : Expr ) extends Expr Like before, this defines a trait Expr , and two concrete subclasses Number and Sum .
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 15 pages?

  • Fall '19

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors