Skip to content

Pattern matching in Scala for Java programmers tutorial #39

@santiagobasulto

Description

@santiagobasulto

Hello friends,

i'm finishing the translation of this tutorial: https://github.com/scala/scala.github.com/blob/gh-pages/tutorials/scala-for-java-programmers.md

When discussing traits, the authors define this method:

override def equals(that: Any): Boolean = that.isInstanceOf[Date] && {
      val o = that.asInstanceOf[Date]
      o.day == day && o.month == month && o.year == year
}

Shouldn't be better to define the same method this way:

   override def equals(that: Any): Boolean = that match{
     case o:Date => o.day == day && o.month == month && o.year == year
     case _ => false
   }

Maybe a note can be added noting the type erasure problem.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions