在
TraversableOnce
中,有一个sum方法只有在包含的类型是Numeric时才可用(否则它将不会编译).我想知道这是否可用于其他情况(以避免运行时检查).
特别是我们有两个特征A和B的情况.我们希望有一个方法f,只有当对象继承A和B时才能使用.但是如果它只扩展其中一个,则不会.我不想做另一个特征AB扩展A与B.我只是想要无法使用f,如果不是两个特征都被继承.
package com.example trait Base trait Foo extends Base { def g = println("foo bar " + toString) } trait Bar extends Base { /* If this is both Foo and Bar,I can do more */ def f = { if (!this.isinstanceOf[Foo]) error("this is not an instance of Foo") this.asInstanceOf[Foo].g } } object Test { def main(args: Array[String]): Unit = { object ab extends Foo with Bar object ba extends Bar with Foo object b extends Bar ab.f ba.f // I don't want next line to compile: try { b.f } catch { case e: RuntimeException => println(e) } } }
编辑:解决方案,感谢@Aaron Novstrup
trait Bar extends Base { self => def f(implicit ev: self.type <:< Foo) = { //self.asInstanceOf[Foo].g // [1] ev(this).g // [2] } }
现在在main中,b.f不编译.尼斯
编辑2:改变行[1]至[2]反映@Aaron Novstrup的回答变化
编辑3:@Aaron Novstrup在没有使用自我反映变化的答案
trait Bar extends Base { /* If this is both Foo and Bar,I can do more */ def f(implicit ev: this.type <:< Foo) = { ev(this).g } }