Why does the following compile? I understand that AnyVal instances correspond to things in the underlying host system that cannot be constructed, and that Null is a subtype of all reference types but not of value types. I have an AnyVal type Boolean I give to safeMapDifferent, but don't see how it can satisfy this constraint of U >: Null.
object MyMainScala extends App { implicit class RichObject[T](o: T) { def safeMap[U](method: T => U)(implicit ev: Null <:< U): U = Option(o).flatMap(result => Option(method(result))).orNull def safeMapDifferent[U >: Null](method: T => U): U = Option(o).flatMap(result => Option(method(result))).orNull } override def main(args: Array[String]): Unit = { val testSubject = new Object() { def scalaBoolean: Boolean = ??? } // println(testSubject.safeMap(_.scalaBoolean)) // If not commented, this will fail to compile as I expect. println(testSubject.safeMapDifferent(_.scalaBoolean).getClass) // Why does it compile? } }