测试方法在类型上不可用的方法
发布时间:2020-12-16 10:07:24 所属栏目:安全 来源:网络整理
导读:给定游戏的类型层次结构,强烈区分下一轮的转弯: trait Gametrait BlackToPlay extends Game { def move(p: BlackPiece,s: Square): Either[FinishedGame,WhiteToPlay]}trait WhiteToPlay extends Game { def move(p: WhitePiece,BlackToPlay]} 我可以在不诉
给定游戏的类型层次结构,强烈区分下一轮的转弯:
trait Game trait BlackToPlay extends Game { def move(p: BlackPiece,s: Square): Either[FinishedGame,WhiteToPlay] } trait WhiteToPlay extends Game { def move(p: WhitePiece,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应该是true,因为b不应该匹配{def c(s:String)}的结构类型 解决方法
您不测试系统已经保证的类型.实际上,类型系统已经是对程序某些属性的测试.
你可以进一步测试你保证某种属性的类型(比如没有玩家连续两次移动),但是这种东西仅限于像Agda和Coq这样的语言. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |