Scala:“模糊的隐式值”,但未找到正确的值

2024-02-18

我正在编写一个小型 Scala 程序,它应该:

  1. 从本地 FS 读取文件(逐行)
  2. 从每一行解析三个双精度值
  3. 根据这三个值创建案例类的实例
  4. 将这些实例传递到二叉堆

为了能够解析Strings 到两个Doubles and CoordinatePoint我想出了这个特点:

trait Parseable[T] {
  def parse(input: String): Either[String, T]
}

我有许多后者的类型对象实现:

object Parseable {
  implicit val parseDouble: Parseable[Double] = new Parseable[Double] {
    override def parse(input: String): Either[String, Double] = {
      val simplifiedInput = input.replaceAll("[ \\n]", "").toLowerCase
      try Right(simplifiedInput.toDouble) catch {
        case _: NumberFormatException =>
          Left(input)
      }
    }
  }

  implicit val parseInt: Parseable[Int] = new Parseable[Int] {
    override def parse(input: String): Either[String, Int] = {
      val simplifiedInput = input.replaceAll("[ \\n]", "").toLowerCase
      try Right(simplifiedInput.toInt) catch {
        case _: NumberFormatException =>
          Left(input)
      }
    }
  }

  implicit val parseCoordinatePoint: Parseable[CoordinatePoint] = new Parseable[CoordinatePoint] {
    override def parse(input: String): Either[String, CoordinatePoint] = {
      val simplifiedInput = input.replaceAll("[ \\n]", "").toLowerCase
      val unparsedPoints: List[String] = simplifiedInput.split(",").toList
      val eithers: List[Either[String, Double]] = unparsedPoints.map(parseDouble.parse)
      val sequence: Either[String, List[Double]] = eithers.sequence
      sequence match {
        case Left(value) => Left(value)
        case Right(doublePoints) => Right(CoordinatePoint(doublePoints.head, doublePoints(1), doublePoints(2)))
      }
    }
  }
}

我有一个公共对象,它将调用委托给相应的隐式对象Parseable(在同一文件中):

object InputParser {
  def parse[T](input: String)(implicit p: Parseable[T]): Either[String, T] = p.parse(input)
}

仅供参考 - 这是CoordinatePoint案例类别:

case class CoordinatePoint(x: Double, y: Double, z: Double)

在我的主程序中(在验证文件存在并且不为空等之后。)我想将每一行转换为一个实例CoordinatePoint如下:

  import Parseable._
  import CoordinatePoint._

  ...
  private val bufferedReader = new BufferedReader(new FileReader(fileName))

  private val streamOfMaybeCoordinatePoints: Stream[Either[String, CoordinatePoint]] = Stream
    .continually(bufferedReader.readLine())
    .takeWhile(_ != null)
    .map(InputParser.parse(_))

我得到的错误是这样的:

[error] /home/vgorcinschi/data/eclipseProjects/Algorithms/Chapter 2 Sorting/algorithms2_1/src/main/scala/ca/vgorcinschi/algorithms2_4/selectionfilter/SelectionFilter.scala:42:27: ambiguous implicit values:
[error]  both value parseDouble in object Parseable of type => ca.vgorcinschi.algorithms2_4.selectionfilter.Parseable[Double]
[error]  and value parseInt in object Parseable of type => ca.vgorcinschi.algorithms2_4.selectionfilter.Parseable[Int]
[error]  match expected type ca.vgorcinschi.algorithms2_4.selectionfilter.Parseable[T]
[error]     .map(InputParser.parse(_))
[error]                           ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 1 s, completed Sep 1, 2020 10:38:18 PM

我不明白也不知道在哪里寻找编译器为什么会发现Parseable[Int] and Parseable[Double]但不是唯一正确的 -Parseable[CoordinatePoint].

所以我想,好吧,让我通过预先指定转换函数来帮助编译器:

  private val bufferedReader = new BufferedReader(new FileReader(fileName))

  val stringTransformer: String => Either[String, CoordinatePoint] = s => InputParser.parse(s)

  private val streamOfMaybeCoordinatePoints: Stream[Either[String, CoordinatePoint]] = Stream
    .continually(bufferedReader.readLine())
    .takeWhile(_ != null)
    .map(stringTransformer)

唉,这会在函数声明中的代码上方产生​​相同的错误。

我很想知道是什么导致了这种行为。既是为了纠正代码,也是为了个人知识。此时我很好奇。


一只狐狸是明确指定类型参数

InputParser.parse[CoordinatePoint](_)

另一个是优先考虑隐含因素。例如

trait LowPriorityParseable1 {
  implicit val parseInt: Parseable[Int] = ...
}

trait LowPriorityParseable extends LowPriorityParseable1 {
  implicit val parseDouble: Parseable[Double] = ...
}

object Parseable extends LowPriorityParseable {
  implicit val parseCoordinatePoint: Parseable[CoordinatePoint] = ...
}

顺便说一句,由于您将隐式放入伴生对象中,因此现在导入它们没有多大意义。

在调用站点中

object InputParser {
  def parse[T](input: String)(implicit p: Parseable[T]): Either[String, T] = p.parse(input)
}

类型参数T推断(如果没有明确指定)之前没有隐式解析(类型推断和隐式解析相互影响)。否则下面的代码将无法编译

trait TC[A]
object TC {
  implicit val theOnlyImplicit: TC[Int] = null
}    
def materializeTC[A]()(implicit tc: TC[A]): TC[A] = tc
  
materializeTC() // compiles, A is inferred as Int

因此,在隐式解析期间,编译器会尝试不太早地推断类型(否则在示例中TC type A将被推断为Nothing并且隐式不会被发现)。顺便说一句,隐式转换是一个例外,编译器会尝试急切地推断类型(有时 https://stackoverflow.com/questions/63002466/what-are-the-hidden-rules-regarding-the-type-inference-in-resolution-of-implicit这也会带来麻烦)

// try to infer implicit parameters immediately in order to:
//   1) guide type inference for implicit views
//   2) discard ineligible views right away instead of risking spurious ambiguous implicits

https://github.com/scala/scala/blob/2.13.x/src/compiler/scala/tools/nsc/typechecker/Implicits.scala#L842-L854 https://github.com/scala/scala/blob/2.13.x/src/compiler/scala/tools/nsc/typechecker/Implicits.scala#L842-L854

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Scala:“模糊的隐式值”,但未找到正确的值 的相关文章

随机推荐