Search code examples
scalatypesequivalentexistential-typetype-alias

Difference between an undefined abstract type member and an existential type


Given an uninitialised abstract type member is =:= equal to an existential type

implicitly[Undefined =:= x forSome { type x }]   // ok

then why there seems to be a difference between them in

object O {
  type Undefined

  implicitly[Undefined =:= _]   // ok

  def g[F[_]](fun: F[_] => F[_]) = ???
  def h[F[_]](fun: F[Undefined] => F[Undefined]) = ???

  g[List](l => List(42))   // ok
  h[List](l => List(42))   // error
}

Note how g compiles whilst h raises type mismatch error. Furthermore consider

object O {
  type Undefined
  type Existential = x forSome { type x }

  implicitly[Undefined =:= x forSome { type x }]   // ok
  implicitly[Undefined =:= Existential]            // error
}

If Undefined equals x forSome { type x }, and x forSome { type x } equals Existential, then why does Undefined not equal Existential?


Solution

  • You missed brackets:

    implicitly[Undefined =:= (x forSome { type x })]
    

    So it doesn't compile.

    There should be difference between them. They are different.

    implicitly[Undefined <:< (x forSome { type x })] 
    

    but not vice versa.

    Actually x forSome { type x } is just Any.

    What is the meaning of implicitly[Undefined =:= _]?

    implicitly[Undefined =:= _] is implicitly[(Undefined =:= x) forSome {type x}].

    And Undefined =:= x is true for some x. Namely for Undefined.