scalatype-constructor

Why does foo[F[_], A](ff: F[A]) accept foo(1)?


Why does the call to foo(1) work in my Scala 2.11.7 repl as described below?

scala> def foo[F[_], A](fa: F[A]) = null
foo: [F[_], A](fa: F[A])Null

scala> foo(List(1))
res0: Null = null

scala> foo(1)
res1: Null = null

The parameter in my call to foo(1) is not a Type Constructor so why does the Scala repl accept it?


Solution

  • foo(1) works because there is an implicit conversion int2Integer to java.lang.Integer

    foo(int2Integer(1))
    

    and Integer is an instance of Comparable[Integer]

    public final class Integer extends Number implements Comparable<Integer>
    

    where we see Comparable is indeed a type constructor. For example, we can reproduce the same behaviour like so

    def foo[F[_], A](fa: F[A]) = 42
    trait Bar // not a type constructor
    implicit def barToComparable(b: Bar): Comparable[Int] = (o: Int) => -1
    val bar = new Bar {}
    foo(bar) // OK due to implicit conversion barToComparable