In [5]:
sealed trait MyOption[A]
case class MySome[A](get: A) extends MyOption[A]
case class MyNone[A]() extends MyOption[A]
In [6]:
def f(a: MyOption[Any]) = a
In [7]:
f(MySome[Any](1))
In [6]:
f(MySome[Int](1))
In [8]:
sealed trait MyOption2[+A]
case class MySome2[A](get: A) extends MyOption2[A]
case class MyNone2[A]() extends MyOption2[A]
In [9]:
def f(a: MyOption2[Any]) = a
In [10]:
f(MySome2[Int](1))
In [11]:
def toS(a: Any) = a.toString
In [12]:
toS(List(1,2,3))
In [13]:
def f(a: String => String, s: String) = a(s)
In [14]:
// This works even though f is String => String
f(toS, "abc")
To understand why take a look at Function1:
trait Function1[-T1, +R] extends AnyRef
which is contravariant in input.
Thanks to @deaktator for explaining these to me