Scala 高级类型的固定类型推理
好的,我有一个非常简单的设置:Scala 高级类型的固定类型推理,scala,type-inference,higher-kinded-types,Scala,Type Inference,Higher Kinded Types,好的,我有一个非常简单的设置: trait Sys[S <: Sys[S]] trait Elem[S <: Sys[S]] trait AttrElem[S <: Sys[S]] { type E <: Elem[S] def attributes: Any def element: E } trait Sys[S以下“冗余”似乎满足了编译器的要求: def apply[S <: Sys[S], E1 <: Elem[S]](elem:
trait Sys[S <: Sys[S]]
trait Elem[S <: Sys[S]]
trait AttrElem[S <: Sys[S]] {
type E <: Elem[S]
def attributes: Any
def element: E
}
trait Sys[S以下“冗余”似乎满足了编译器的要求:
def apply[S <: Sys[S], E1 <: Elem[S]](elem: E1 with Elem[S]):
AttrElem[S] { type E = E1 } = ...
def apply[S是f***
f-bounded polymorphic的缩写
?这是一种称为System f***(三星)的形式主义。你能用“人类语言”描述一下你想要实现什么吗?我很难推断Sys/Elem/AttrElem的含义。我想能够写出工厂(Elem)
当elem
完全已知时,一个子类型elem[S]
。无需键入Factory[S,某些子类型的elem[S]](elem)
。换句话说,我想使用类型推断的广告功能这里是相同问题的另一个实例:
def test[S <: Sys[S]](elem: Elem[S]): Unit = {
Factory(elem)
}
<console>:62: error: inferred type arguments [Nothing,Elem[S]] do not conform
to method apply's type parameter bounds [S <: Sys[S],E1 <: Elem[S]]
Factory(elem)
^
object Factory {
def apply[S <: Sys[S], E1[~] <: Elem[~] forSome { type ~ <: Sys[~] }](
elem: E1[S]): AttrElem[S] { type E = E1[S] } = new Impl(elem)
private class Impl[S <: Sys[S], E1[~] <: Elem[~] forSome { type ~ <: Sys[~] }](
val element: E1[S]) extends AttrElem[S] {
type E = E1[S]
def attributes = 1234
}
}
<console>:62: error: inferred kinds of the type arguments (S,E1[S]) do not
conform to the expected kinds of the type parameters (type S,type E1) in
class Impl.
E1[S]'s type parameters do not match type E1's expected parameters:
type E1 has one type parameter, but type E1 (in class Impl) has one
elem: E1[S]): AttrElem[S] { type E = E1[S] } = new Impl(elem)
^
def apply[S <: Sys[S], E1 <: Elem[S]](elem: E1 with Elem[S]):
AttrElem[S] { type E = E1 } = ...