假设我具有代表类似多态函数的特征,例如:
trait Func[A[X, Y]] {
def apply[X, Y](a: A[X, Y]): A[X, Y]
}
现在我想通过将lambda类型作为参数来将我的特征用作非多态函数:
type single[T] = { type t[X, Y] = T }
val aInstance: Func[single[String]#t] =
new Func[single[String]#t] {
def apply[X, Y](a: String): String = ???
}
现在我有了方法test
,它可以对func
做一些有用的事情,例如
def test[A[X, Y]](f: Func[A]): Unit = ???
我想用test
调用aInstance
而不用手指定类型参数:
test(aInstance)
不幸的是,此代码无法编译(但test[single[String]#t](aInstance)
可以)并显示错误消息:
[error] /test.scala:16:3: no type parameters for method test: (f: Func[A])Unit exist so that it can be applied to arguments (Func[[X, Y]String])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : Func[[X, Y]String]
[error] required: Func[?A]
[error] test(aInstance)
[error] ^
[error] /test.scala:16:8: type mismatch;
[error] found : Func[[X, Y]String]
[error] required: Func[A]
[error] test(aInstance)
[error] ^
[error] two errors found
我的问题是:如何修改这些声明以允许编译器自动推断所有必需的类型?
对于那些想知道为什么我将Func
声明为具有[X, Y]
但从未在实际代码中使用它们的人,有一个更真实的世界,更少抽象的示例:
object GenericTest {
trait Item { def name: String }
class ItemA extends Item { def name: String = "a" }
class ItemB extends Item { def name: String = "b" }
trait MapFn[-A[X <: Item], +B[X <: Item]] {
def apply[X <: Item](data: A[X]): B[X]
}
case class ItemsCollection[C[A <: Item]](a: C[ItemA], b: C[ItemB]) {
def map[D[A <: Item]](f: MapFn[C, D]): ItemsCollection[D] =
ItemsCollection(f(a), f(b))
}
// sometimes we want to store sequences...
val itemSeq = ItemsCollection[Seq](Seq(new ItemA), Seq(new ItemB))
// or transform them:
val itemSet = itemSeq.map(new MapFn[Seq, Set] {
override def apply[X <: Item](data: Seq[X]): Set[X] = data.toSet
})
// but one day we wanted to store just objects without any item-specific types... e.g. names:
type single[X] = { type t[A] = X }
val itemNames = itemSeq.map(new MapFn[Seq, single[String]#t] {
override def apply[X <: Item](data: Seq[X]): String = data.head.name
})
/*
[error] test.scala:28:27: no type parameters for method map: (f: MapFn[Seq,D])ItemsCollection[D] exist so that it can be applied to arguments (MapFn[Seq,[A]String])
[error] --- because ---
[error] argument expression's type is not compatible with formal parameter type;
[error] found : MapFn[Seq,[A]String]
[error] required: MapFn[Seq,?D]
[error] val itemNames = itemSeq.map(new MapFn[Seq, single[String]#t] {
[error] ^
[error] test.scala:28:31: type mismatch;
[error] found : MapFn[Seq,[A]String]
[error] required: MapFn[Seq,D]
[error] val itemNames = itemSeq.map(new MapFn[Seq, single[String]#t] {
[error] ^
[error] two errors found
*/
}
答案 0 :(得分:3)
参考GenericTest
,由于this closed-but-unfixed bug,无法使Scala推断出该形状。
您可以做的一件事就是尝试改编the technique of Unapply,使用隐式分辨率来确定D
的可能候选者。这可能需要定义自己的类型类和实例,而不使用Scalaz提供的类型和实例,并可能更改声明MapFn
的方式更适合此模式。确保为您提供single
的实例具有最低的优先级,因为它可以随时使用(如果T
,则每个F[T]
都是F = Id
)。
如果控制MapFn
的定义,也可以将B
类型参数移至类型成员。然后map
的签名变为
def map(f: MapFn[C]): ItemsCollection[f.B] =
您可以向MapFn
添加一个子类,该子类将类型成员移回参数,以便于创建MapFn
。