2016-06-12 51 views
0
sealed trait Option_40to49[+A] { 
    def map[B](f: A => B): Option[B] = this match { 
    case None => None 
    case Some(x) => Some(f(x)) 
    } 
} 

我在日食工作的匹配,它强调无下一个错误:阶格局特征

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

,并与一些(X)

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

为什么我有这样的类似问题?如何解决它?

回答

2

通过使用你的模式匹配this你指的是你的Option_40to49,但因为你还没有实现NoneSome编译器不知道这些是什么

的这些简单的版本不是很难实现自己。请注意,您还需要将输出更改为mapOption_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]