scala – 参数化类型的隐式类解析

前端之家收集整理的这篇文章主要介绍了scala – 参数化类型的隐式类解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在下面的示例中,似乎 Scala编译器仅在识别为隐式类时才识别Wrapper的高级表示.这是为什么?

@H_403_11@scala> case class Nested(n: Int) defined class Nested scala> case class Wrapper[A <: Product](nested: A) defined class Wrapper scala> implicit class I1[W <: Wrapper[A],A <: Product](underlying: W) { | def ok1() = true | } defined class I1 scala> Wrapper(Nested(5)).ok1() <console>:26: error: value ok1 is not a member of Wrapper[Nested] Wrapper(Nested(5)).ok1() ^ scala> implicit class I2[W <: Wrapper[_]](underlying: W) { | def ok2() = true | } defined class I2 scala> Wrapper(Nested(5)).ok2() res1: Boolean = true

隐式解决方案是否有一个解决方法来维护有关嵌套类型的完整信息,允许将类型类证据(例如TypeTag)附加到它上面?

注意:上面的示例显示Nested和Wrapper是案例类,但这不是问题的组成部分.它只是为更短更简单的控制台会话提供便利.

解决方法

由于Scala类型推断的限制,这种情况正在发生.见 SI-2272.

隐式无法解析,因为编译器无法正确推断A.如果我们启用-Xlog-implicits,我们可以看到这一点.请注意,A被推断为Nothing:

@H_403_11@I1 is not a valid implicit value for Test.w.type => ?{def ok: ?} because: inferred type arguments [Wrapper[Nested],Nothing] do not conform to method I1's type parameter bounds [W <: Wrapper[A],A <: Product]

如果我们尝试手动实例化I1,会发生同样的事情:

@H_403_11@scala> val w = Wrapper(Nested(5)) w: Wrapper[Nested] = Wrapper(Nested(5)) scala> new I1(w) <console>:21: error: inferred type arguments [Wrapper[Nested],Nothing] do not conform to class I1's type parameter bounds [W <: Wrapper[A],A <: Product] new I1(w) ^ <console>:21: error: type mismatch; found : Wrapper[Nested] required: W new I1(w) ^

现在,解决方法.

首先,Wrapper是一个案例类,因此它没有理由让它有子类型.您可以删除W类型参数,并将底层更改为Wrapper [A]:

@H_403_11@implicit class I1[A <: Product](underlying: Wrapper[A]) { def ok = true }

如果您仍然希望需要两个类型参数,您还可以要求隐含证据表明W<:<包装器[A],同时删除类型参数W的上限:

@H_403_11@implicit class I1[W,A <: Product](underlying: W)(implicit ev: W <:< Wrapper[A]) { def ok = true }

猜你在找的Scala相关文章