Nikita
Nikita

Reputation: 1082

scala pattern matching in trait

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

I work in eclipse, it underlined None with the next error:

pattern type is incompatible with expected type; found : None.type required: packageName.Option_40to49[A]

and the similar with Some(x)

constructor cannot be instantiated to expected type; found : Some[A(in class Some)] required: packageName.Option_40to49[A(in trait Option_40to49)]

Why I have this problem? How to fix it?

Upvotes: 0

Views: 860

Answers (1)

Hamish
Hamish

Reputation: 1015

By using this in your pattern match you're referring to your Option_40to49, but since you haven't implemented None or Some the compiler doesn't know what these are

Simple versions of these aren't that hard to implement yourself. Note, you'll also want to change your output for map to Option_40to49

sealed trait Option_40to49[+A] {
  def map[B](f: A => B): Option_40to49[B] = this match {
    case None => None
    case Some(x) => Some(f(x))
  }
}

case class Some[A](x: A) extends Option_40to49[A]
case object None extends Option_40to49[Nothing]

Upvotes: 2

Related Questions