gpt4 book ai didi

scala - 在 Scala : how to get compiler to infer types correctly? 中将类型 lambda 与更高级的类型一起使用

转载 作者:行者123 更新时间:2023-12-01 01:42:44 24 4
gpt4 key购买 nike

假设我有代表多态函数之类的特征,例如:

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

现在我有了方法 testfunc 做了一些有用的事情,例如
def test[A[X, Y]](f: Func[A]): Unit = ???

我想调用 testaInstance无需手动指定类型参数:
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
*/
}

最佳答案

引用您的 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创建。

关于scala - 在 Scala : how to get compiler to infer types correctly? 中将类型 lambda 与更高级的类型一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54767798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com