微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

scala – 如何在编译时进行类型检查?

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
  }
}

解决方法

是的你可以:

trait A {
   def bar = println("I'm an A!")
}

trait B { 
   def foo(implicit ev: this.type <:< A) = { 
      ev(this).bar
      println("and a B!")
   }
}

如果对象的静态类型(在调用站点)扩展A,编译器将只能提供证据参数.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐