我试图在 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))
为什么这不起作用?我不确定为什么会收到重载方法错误。