Why does foo[F[_], A](ff: F[A]) accept foo(1)?
Your Int
is considered an instance of class Any
(because is not a type constructor and so type hierarchy is analyzed to find a supertype that is also a type constructor), and class Any
in Scala is considered a type constructor of type Nothing
.
You can check this behavior with following code:
import scala.reflect.runtime.universe._
object Main {
def foo[F[_], A](fa: F[A])(implicit ev: TypeTag[F[A]], ev2: TypeTag[A]) = {
println(ev)
println(ev2)
println(ev.tpe.typeArgs)
println()
null
}
def main(args: Array[String]){
foo(List(1))
foo(1)
}
}
Output:
TypeTag[List[Int]]
TypeTag[Int]
List(Int)
TypeTag[Any]
TypeTag[Nothing]
List()