scala - 隐式类和“不是类型参数的成员”错误
问题描述
我有以下类型类定义 -
trait ToBigInt[A] {
def toBigInt(n: A): BigInt
}
object ToBigInt {
def apply[A](implicit t: ToBigInt[A]): ToBigInt[A] = t
implicit val toBigIntByte: ToBigInt[Byte] = (x: Byte) => BigInt(x)
implicit val toBigIntShort: ToBigInt[Short] = (x: Short) => BigInt(x)
implicit val toBigIntInt: ToBigInt[Int] = (x: Int) => BigInt(x)
implicit val toBigIntLong: ToBigInt[Long] = (x: Long) => BigInt(x)
implicit val toBigIntBigInt: ToBigInt[BigInt] = identity _
implicit class RichToBigInt[A: ToBigInt](value: A) {
def toBigInt: BigInt = ToBigInt[A].toBigInt(value)
}
}
值得注意的是,在ToBigInt
对象中,我包含了一个包含该toBigInt
方法的隐式类。
不幸的是,以下代码无法编译 -
import FromBigInt._
import ToBigInt._
final class Unsigned[A: BoundedIntegral] private (val value: A) {
def toBigInt: BigInt =
BoundedIntegral[A].toBigInt(value) - BoundedIntegral[A].toBigInt(BoundedIntegral[A].minValue)
override def toString: String = toBigInt.toString
override def equals(that: Any): Boolean = that match {
case unsigned: Unsigned[A] => toBigInt == unsigned.toBigInt
case _ => false
}
}
object Unsigned {
def apply[A: BoundedIntegral, N: ToBigInt](n: N): Unsigned[A] = {
assert(n.toBigInt >= BigInt(0), "Integer cannot be negative")
assert(n.toBigInt <= BoundedIntegral[A].maxValue.toBigInt * 2 + 1, "Integer out of range")
new Unsigned[A]((n.toBigInt - BoundedIntegral[A].minValue.toBigInt).fromBigInt)
}
}
trait BoundedIntegral[A] extends Bounded[A] with ToFromBigInt[A]
object BoundedIntegral {
def apply[A](implicit b: BoundedIntegral[A]): BoundedIntegral[A] = b
}
[error] Unsigned.scala:24:14: value toBigInt is not a member of type parameter N
[error] assert(n.toBigInt >= BigInt(0), "Integer cannot be negative")
[error] ^
[error] Unsigned.scala:25:14: value toBigInt is not a member of type parameter N
[error] assert(n.toBigInt <= BoundedIntegral[A].maxValue.toBigInt * 2 + 1, "Integer out of range")
[error] ^
[error] Unsigned.scala:25:54: value toBigInt is not a member of type parameter A
[error] assert(n.toBigInt <= BoundedIntegral[A].maxValue.toBigInt * 2 + 1, "Integer out of range")
[error] ^
[error] Unsigned.scala:26:24: value toBigInt is not a member of type parameter N
[error] new Unsigned[A]((n.toBigInt - BoundedIntegral[A].minValue.toBigInt).fromBigInt)
[error] ^
[error] Unsigned.scala:26:63: value toBigInt is not a member of type parameter A
[error] new Unsigned[A]((n.toBigInt - BoundedIntegral[A].minValue.toBigInt).fromBigInt)
另一方面,intellij 的代码没有问题。这段代码有什么问题?
更新:我已经缩小了问题的范围 -
package unsigned
trait ToBigInt[A] {
def toBigInt(n: A): BigInt
}
object ToBigInt {
def apply[A](implicit t: ToBigInt[A]): ToBigInt[A] = t
implicit class RichToBigInt[A: ToBigInt](value: A) {
def toBigInt: BigInt = ToBigInt[A].toBigInt(value)
}
def test1[A: ToBigInt](n: A): BigInt = n.toBigInt
}
trait FromBigInt[A] {
def fromBigInt(n: BigInt): A
}
object FromBigInt {
def apply[A](implicit t: FromBigInt[A]): FromBigInt[A] = t
implicit class RichToBigInt(value: BigInt) {
def fromBigInt[A: FromBigInt]: A = FromBigInt[A].fromBigInt(value)
}
}
object Test1 {
import ToBigInt._
def test1[A: ToBigInt](n: A): BigInt = n.toBigInt
}
object Test2 {
import ToBigInt._
import FromBigInt._
def test2[A: ToBigInt](n: A): BigInt = n.toBigInt
}
对象中未使用的导入import FromBigInt._
导致Test2
编译时错误。
....:37:44: value toBigInt is not a member of type parameter A
[error] def test2[A: ToBigInt](n: A): BigInt = n.toBigInt
[error] ^
[error] one error found
解决方案
我认为您没有按照他们应该的方式使用隐式。
apply
在Unsigned
as中声明方法def apply[A <: BoundedIntegral[_], N](n: N)(implicit bigInt: ToBigInt[N])
似乎修复了编译的这一部分
推荐阅读
- dictionary - 使用传单按距离搜索标记
- python - pandas 按 timedelta64[ns] 列加入连续行分组
- python - 我该如何解决这个错误?类型错误:_append_dispatcher() 缺少 1 个必需的位置参数:“值”
- html - 如何使用css在图像底部创建形状三角形
- spring - 如何根据 Spring-Boot 创建专门用于测试和创建实体管理器的数据库表
- visual-studio-code - 在 C++ VSCODE 编辑器中键入字长
- asp.net - 为什么我的 asp.net 模型绑定不起作用?
- asp.net - 如何从 ASP.NET Core 的授权回退策略中排除静态文件
- rust - 当提取函数的返回类型是根据特征定义的时,如何在 Rust 中提取函数的一部分?
- php - Wordpress如何检查查询是否有帖子