2016-06-12 41 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]