2017-02-24 9 views
1

私は再度学習するためにscalaのSomeとNoneを再実装しようとしています。私はscalaワークスペースで次のコードをすべて実行します。スカラ:パターンマッチングのcaseクラスのメソッドエラーを解決できません

Cannot resolve method name unapply

が、私はこのことを理解していない:

case class Some[+A](get: A) extends Option[A] 
case object None extends Option[Nothing] 

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

は、私は次のエラーを満たしています。このエラーは、通常のクラスでのみ発生することが多いためです。なぜ私がこのエラーに会うのか教えてください。

  • scala._
  • java.lang._
  • scala.Predef._

これらはOptionクラスとそのサブタイプのものがあります。それは、次のシーンの後ろに輸入しているため

+1

REPLで完全に動作します。 –

答えて

4

私は、通訳だけで混乱していると思います - SomeおよびNone。私はあなたのクラスの名前をちょうど少し変更して、すべて正常に動作します:

trait Optional[+A] { 
    def map[B](f: A => B): Optional[B] = this match { 
    case Absent => Absent 
    case Present(a) => Present(f(a)) 
    } 
} 
case class Present[+A](get: A) extends Optional[A] 
case object Absent extends Optional[Nothing] 

Present(3).map(_ * 2) // Present(6) 
関連する問題