trait Binding[InternalType, ExternalType]
object BooleanBinding extends Binding[Boolean, String]
class Field(val binding : Binding[_,_], val name : String)
val field = new Field(BooleanBinding, "name")
给出字段,如何获取Binding的InternalType,在这种情况下为Boolean。
答案 0 :(得分:2)
您可以通过反射获得类型。由于类型擦除,必须使用TypeTag。
import scala.reflect.runtime.universe._
trait Binding[InternalType, ExternalType]
object BooleanBinding extends Binding[Boolean, String]
class Field[A:TypeTag, B:TypeTag](val binding : Binding[A,B], val name : String) {
val InternalType = typeOf[A]
val ExternalType = typeOf[B]
}
private val name = new Field(BooleanBinding, "name")
println(name.InternalType)
// Boolean
println(name.ExternalType)
// String