我有一组类型,每个类型都有自己的类型成员:
sealed trait FieldType {
type Data
def parse(in: String): Option[Data]
}
object Name extends FieldType {
type Data = String
def parse(in: String) = Some(in)
}
object Age extends FieldType {
type Data = Int
def parse(in: String) = try { Some(in.toInt) } catch { case _ => None }
}
我有一组类型在集合上运行FieldType
s(使用样板而不是对数量进行抽象):
sealed trait Schema {
type Schema <: Product
type Data <: Product
val schema: Schema
def read(in: Seq[String]): Option[Data]
}
trait Schema1 extends Schema {
type D1
type FT1 <: FieldType { type Data = D1 }
type Schema = Tuple1[FT1]
type Data = Tuple1[D1]
def read(in: Seq[String]) = schema._1.parse(in(0)).map(Tuple1.apply)
}
trait Schema2 extends Schema {
type D1
type D2
type FT1 <: FieldType { type Data = D1 }
type FT2 <: FieldType { type Data = D2 }
type Schema = (FT1, FT2)
type Data = (D1, D2)
def read(in: Seq[String]) = {
for {
f <- schema._1.parse(in(0))
s <- schema._2.parse(in(1))
} yield (f, s)
}
}
我想我可以使用这个系统来优雅地定义有意义的字段集,因为 scala 能够推断类型成员:
class Person extends Schema2 {
val schema = (Name, Age)
}
但是,这不能编译!我必须包含所有类型成员的定义:
class Person extends Schema2 {
type D1 = String; type D2 = Int
type FT1 = Name.type; type FT2 = Age.type
val schema = (Name, Age)
}
为什么 scala 无法推断 D1,... 和 FT1,...?我怎样才能重构它,这样我就不必在中指定类型变量Person
?
注意:一旦我对宏有了更好的理解,我计划将它们用于Schema
类型。另外,我不想使用无形的。这是一个很棒的库,但我不想用它来解决这个问题。