【发布时间】:2020-10-12 01:50:38
【问题描述】:
我想构建一个转换方法以通过构建器输出特定类型,但根据情况,最后一个是否采用隐式位置会修改泛型类型自动推断。我很乐意了解更多有关的好奇行为。
trait Wrap[A]
trait Toto[A, W <: Wrap[A]] {
def fail[A2, W2 <: Wrap[A2], T2 <: Toto[A2, W2]](f: W => W2)(implicit builder: Builder[A2, W2, T2]): T2
def works[A2, W2 <: Wrap[A2], T2 <: Toto[A2, W2]](f: W => W2, builder: Builder[A2, W2, T2]): T2
}
trait Builder[A, W <: Wrap[A], T <: Toto[A, W]]
type A1
type W1 <: Wrap[A1]
type A2
type W2 <: Wrap[A2]
type T2 <: Toto[A2, W2]
val t1: Toto[A1, W1]
implicit val builder: Builder[A2, W2, T2]
val f: W1 => W2
t1.fail(f) // inferred type arguments [Nothing, W2, Nothing] do not conform to method fail's type parameter
t1.fail(f)(builder) // inferred type arguments [Nothing, W2, Nothing] do not conform to method fail's type parameter
/**
* [error] found : Test.this.W1 => Test.this.W2
* [error] required: Test.this.W1 => W2
*/
t1.works(f, builder) // compiles
【问题讨论】:
-
是的,这是故意的,因为我想他们在这种情况下没有发挥作用。
标签: scala type-inference implicit