在 Scala 检查中为运行时类型启用隐式导入。 "could not find implicit value for parameter"
enable implicit import for runtime type in scala check. "could not find implicit value for parameter"
我正在使用 ScalaCheck
库和 ScalaTest
在 Scala 中测试我自己的 home-brewed Monoid
类
在尝试实施 DRY 测试时,我在标题中收到隐式错误:
Error:(16, 12) could not find implicit value for parameter arbA: org.scalacheck.Arbitrary[A]
forAll { (a: A) =>
^
这里是 intAddition
幺半群的实现:
trait Monoid[A] {
def op(a1: A, a2: A): A
def zero: A
}
object Monoid {
...
val intAddition: Monoid[Int] = new Monoid[Int] {
override def op(a1: Int, a2: Int): Int = a1 + a2
override def zero: Int = 0
}
...
}
和测试套件:
import org.fpinscala.monoids.Monoid._
import org.fpinscala.testutils.UnitSpec
import org.scalatest.prop.PropertyChecks
import org.scalacheck.Arbitrary._
import scala.language.implicitConversions
class MonoidSpec extends UnitSpec with PropertyChecks {
def assertIdentityBehaviour[A](M: Monoid[A]): Unit = {
import M._
forAll { (a: A) =>
op(zero, a) should be(a)
op(a, zero) should be(a)
}
}
behavior of "intAdditionMonoid"
it should "obey identity laws" in {
assertIdentityBehaviour(intAddition)
}
}
此代码编译但在运行时失败(运行时类型擦除?)。
我想在 Scala 中实现什么?
This code compiles
没有;你给出的错误是编译错误。它应该通过添加它抱怨的隐式参数来修复:
def assertIdentityBehaviour[A](M: Monoid[A])(implicit arbA: Arbitrary[A]) = ...
// or equivalently, def assertIdentityBehaviour[A: Arbitrary](M: Monoid[A]) = ...
您仅 调用 assertIdentityBehaviour
A
参数可用,但错误在其 定义中.
我正在使用 ScalaCheck
库和 ScalaTest
Monoid
类
在尝试实施 DRY 测试时,我在标题中收到隐式错误:
Error:(16, 12) could not find implicit value for parameter arbA: org.scalacheck.Arbitrary[A]
forAll { (a: A) =>
^
这里是 intAddition
幺半群的实现:
trait Monoid[A] {
def op(a1: A, a2: A): A
def zero: A
}
object Monoid {
...
val intAddition: Monoid[Int] = new Monoid[Int] {
override def op(a1: Int, a2: Int): Int = a1 + a2
override def zero: Int = 0
}
...
}
和测试套件:
import org.fpinscala.monoids.Monoid._
import org.fpinscala.testutils.UnitSpec
import org.scalatest.prop.PropertyChecks
import org.scalacheck.Arbitrary._
import scala.language.implicitConversions
class MonoidSpec extends UnitSpec with PropertyChecks {
def assertIdentityBehaviour[A](M: Monoid[A]): Unit = {
import M._
forAll { (a: A) =>
op(zero, a) should be(a)
op(a, zero) should be(a)
}
}
behavior of "intAdditionMonoid"
it should "obey identity laws" in {
assertIdentityBehaviour(intAddition)
}
}
此代码编译但在运行时失败(运行时类型擦除?)。 我想在 Scala 中实现什么?
This code compiles
没有;你给出的错误是编译错误。它应该通过添加它抱怨的隐式参数来修复:
def assertIdentityBehaviour[A](M: Monoid[A])(implicit arbA: Arbitrary[A]) = ...
// or equivalently, def assertIdentityBehaviour[A: Arbitrary](M: Monoid[A]) = ...
您仅 调用 assertIdentityBehaviour
A
参数可用,但错误在其 定义中.