I want to understand why inlining function fixes runtime error.
The following code results in a runtime error (see bellow)
[error] java.lang.ClassCastException: class Herbivore cannot be cast to class Food (Herbivore and Food are in unnamed module of loader sbt.internal.LayeredClassLoader @4837dd91)
trait GameObject
case class Food(x: Int) extends GameObject
case class Herbivore() extends GameObject
@main
def main: Unit =
val xs = Vector(Food(1), Herbivore(), Food(2))
def minBy[T <: GameObject](f: T => Double): Option[T] =
xs.collect{case v: T => v}.minByOption(f)
val done = minBy[Food](food => food.x)
println(done)
until i change it so minBy is inlined and works as intended(outputs: Some(Food(1))
).
inline def minBy[T <: GameObject](f: T => Double): Option[T] = //-snip-
Is this intended behaviour?