336062 (2) [Avatar] Offline
#1
Hi,

I was wondering why all the methods in errorhandling section were defined in the trait instead of in the actual case classes?

sealed trait Option[+A] {
  def map[B](f: A => B): Option[B] = this match {
    case None => None
    case Some(a) => Some(f(a))
  }
// ...
}

Is this the proper functional way?

Thanks,
Tamas
runar (53) [Avatar] Offline
#2
It is just easier to see, since then the definition of these functions are not scattered among the case classes. Either way works in this particular case.

But beware when you start to make recursive definitions. A recursive definition on cases in the trait can be tail-recursive, but mutually recursive definitions in the case classes cannot.
336062 (2) [Avatar] Offline
#3
Yeah, later I found out that it's the design decision between functional or oo.

Thanks for your answer,
Tamas