次の順番を強く区別するゲームのタイプ階層を考えると、次のようになります。
trait Game
trait BlackToPlay extends Game {
def move(p: BlackPiece, s: Square): Either[FinishedGame, WhiteToPlay]
}
trait WhiteToPlay extends Game {
def move(p: WhitePiece, s: Square): Either[FinishedGame, BlackToPlay]
}
反省に頼ることなく、次の重要な主張をすることはできますか?
"A game with white to play" should {
"not allow black to play" in {
// an instance of whiteToPlay should not
// have the `move(BlackPiece, Square)` method.
}
}
編集:@Martinのソリューションを実装しようとしてもうまくいきません。ここで何が悪いのかについて何か考えはありますか?REPLから:
scala> class B() {
| def b(s: String) = s
| }
defined class B
scala> val b = new B()
b: B = B@420e44
scala> b.c("")
<console>:8: error: value c is not a member of B
b.c("")
^
scala> b match {
| case _: { def c(s: String) } => false
| case _ => true
| }
warning: there were unchecked warnings; re-run with -unchecked for details
res7: Boolean = false
res7
b
の構造タイプと一致してはならないため、真である必要があります{ def c(s: String) }