2
votes

I'm trying to generate an arbitrary function of the form f(x) = ax + b, where a and b are arbitrary integers, in ScalaCheck. How do I do this?

I tried:

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 })
}

but I get an error:

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))

Why doesn't this work? I'm not sure why I'm getting an overloaded method error.

1

1 Answers

4
votes
def arbitraryFunction[Int]

this defines a function with a type paramter called Int. As far as I understand your problem, your function does not need any type parameters. The compiler error is caused by this type identifier, which shadows the "normal" Int identifier. Just remove it and your code will work, unless it contains other flaws.