[英]Runtime “could not find implicit value for parameter” error when using Scala's builder idiom
I am writing a Scala class that implements a 2-dimensional matrix of arbitrary objects. 我正在编写一个Scala类,该类实现任意对象的二维矩阵。 I need the class to be more specialized than nested pair of
IndexedSeq
objects, but extending a collections class is overkill, so I'm writing my own. 我需要的
IndexedSeq
嵌套的IndexedSeq
对象对更专业,但是扩展collections类太过分了,所以我要编写自己的类。 In order to return the correct type from methods in my matrix class, I am using the implicit builder idiom, but at runtime I get a "could not find implicit value for parameter" error which I don't understand. 为了从矩阵类中的方法返回正确的类型,我使用了隐式生成器习惯用法,但是在运行时,我收到了“无法为参数找到隐式值”错误,我不理解。
A stripped-down version of my matrix class looks like this. 我的矩阵类的精简版本如下所示。
trait MatrixBuilder[V, M <: Matrix[V]] {
def apply(values: IndexedSeq[IndexedSeq[V]]): M
}
abstract class Matrix[V](values: IndexedSeq[IndexedSeq[V]]) extends Function2[Int, Int, V] {
def apply(row: Int, col: Int): V = values(row)(col)
def set[M <: Matrix[V]](row: Int, col: Int, value: V)(implicit builder: MatrixBuilder[V, M]): M =
builder(values.updated(row, values(row).updated(col, value)))
}
case class IntMatrix(values: IndexedSeq[IndexedSeq[Int]]) extends Matrix[Int](values)
object IntMatrix {
def apply(n: Int) = new IntMatrix(IndexedSeq.fill(n, n)(0))
implicit object IntMatrixBuilder extends MatrixBuilder[Int, IntMatrix] {
def apply(values: IndexedSeq[IndexedSeq[Int]]) = IntMatrix(values)
}
}
I want the set
function to set the specified cell then return a new matrix of the correct type. 我希望
set
函数设置指定的单元格,然后返回正确类型的新矩阵。 So I expect IntMatrix(2).set(0,0,5)
to return an IntMatrix
object with zeros in all cells except (0,0), where it should have a 5. Instead I get the following error at runtime. 因此,我希望
IntMatrix(2).set(0,0,5)
返回一个除(0,0)以外的所有单元中都为零的IntMatrix
对象,该对象应具有5。相反,在运行时出现以下错误。
error: could not find implicit value for parameter builder: MatrixBuilder[Int,M]
IntMatrix(2).set(0,0,5)
What am I doing wrong here? 我在这里做错了什么?
As pedrofurla notes below, the code does work in the REPL if you first run the line import IntMatrix._
. 如下pedrofurla所述,如果您首先运行
import IntMatrix._
行,则该代码在REPL中import IntMatrix._
。 And looking at the collections documentation , there appear to be similar import statements in source code using builders. 并查看集合文档 ,使用构建器的源代码中似乎有类似的import语句。 I tried adding one to my
IntMatrix
class. 我尝试将一个添加到我的
IntMatrix
类。
case class IntMatrix(values: IndexedSeq[IndexedSeq[Int]]) extends Matrix[Int](values) {
import IntMatrix._
}
But this has no effect. 但这没有效果。 (In fact my IDE IntelliJ flags this as an unused import statement.)
(实际上,我的IDE IntelliJ将其标记为未使用的import语句。)
For comparison I copied over the RNA sequence example from the collections documentation linked above verbatim. 为了比较,我复制了逐字链接的馆藏文档中的RNA序列示例。 There the
import RNA._
line is not marked as superfluous and all operations return the correct type. import RNA._
, import RNA._
行未标记为多余,并且所有操作均返回正确的类型。 If the answer is that I need to add an import IntMatrix._
, I can't figure out where to put it. 如果答案是我需要添加一个
import IntMatrix._
,我不知道该放在哪里。
This little code worked here: 这个小代码在这里工作:
scala> import IntMatrix._
import IntMatrix._
scala> IntMatrix(2).set(0,0,5)
res1: Mat.IntMatrix = <function2>
Implicit parameters are filled by compiler in the call site, so they have to be available in the scope set
is being invoked. 隐式参数由编译器在调用站点中填充,因此它们必须在被调用的作用域
set
可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.