我试图在 ScalaCheck 中生成 f(x) = ax + b 形式的任意函数,其中 a 和 b 是任意整数。我该怎么做?
我尝试过:
def arbitraryFunction[Int] = Arbitrary {
for (
a <- Arbitrary.arbInt.arbitrary;
b <- Arbitrary.arbInt.arbitrary
) yield (new Function1[Int, Int] { def apply(x : Int): Int = a * x + b })
}
但我收到错误:
overloaded method value * with alternatives:
[error] (x: Double)Double <and>
[error] (x: Float)Float <and>
[error] (x: Long)Long <and>
[error] (x: scala.Int)scala.Int <and>
[error] (x: Char)scala.Int <and>
[error] (x: Short)scala.Int <and>
[error] (x: Byte)scala.Int
[error] cannot be applied to (Int(in method arbitraryFunction))
为什么这不起作用?我不确定为什么会出现重载方法错误。
最佳答案
def arbitraryFunction[Int]
这定义了一个带有名为Int
的类型参数的函数。据我了解您的问题,您的函数不需要任何类型参数。编译器错误是由该类型标识符引起的,它掩盖了“正常”Int
标识符。只要删除它,您的代码就可以工作,除非它包含其他缺陷。
关于scala - 在 ScalaCheck 中生成任意线性函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12035017/