Reputation: 24593
I'm playing with Scala reflection and ScalaTest. I've a method defined as follows in an object Ch2
:
def isSorted[A](as: Array[A], ordered: (A, A) => Boolean): Boolean
The following test fails as described in the comment:
"Method isSorted" should "return true for a sorted array" in {
val methods =
Table(
("method"),
("isSorted")
)
val m = ru.runtimeMirror(getClass.getClassLoader)
val mod = ru.typeOf[Ch2.type].termSymbol.asModule
val mm = m.reflectModule(mod)
val obj = mm.instance
val im = m.reflect(obj)
forAll(methods) { (m: String) =>
val isSortedMethod = ru.typeOf[Ch2.type].decl(ru.TermName(m)).asMethod
val isSorted = im.reflectMethod(isSortedMethod)
// Fails at runtime with 'missing parameter type for expanded function'
isSorted(Array(1, 2, 3, 4), Ordering[Int].lt(_, _))
}
}
Of course, I could replace Ordering[Int].lt
with (x: Int, y: Int) => x < y
and it'd work but I'd rather use what's already provided instead of rolling my own.
Upvotes: 0
Views: 48
Reputation: 8462
The compiler cannot infer type A
in your call. Try to make the call without reflection.
You should specify type during call like
isSorted(Array(1, 2, 3, 4), (x: Int, y: Int) => Ordering[Int].lt(x, y))
Upvotes: 1