Search code examples
scalatypeshigher-kinded-typestype-equivalencepartially-applied-type

Scala: issue with partially applied type constructors


I have defined the following type Alias:

type PartiallyAppliedEither[A] = Either[String, A]

I can see that the following compiles:

def checkTypeEquality1(implicit ev: PartiallyAppliedEither[_] =:= Either[String, _]) = 1

checkTypeEquality1 //compiles

but these don't:

def checkTypeEquality2[A](implicit ev: PartiallyAppliedEither[A] =:= Either[String, A]) = 1 

checkTypeEquality2 //fails to compile.

def checkTypeEquality3[A](implicit ev: PartiallyAppliedEither[A] =:= Either[String, _]) = 1

checkTypeEquality3 //fails to compile.

Why is the first equality resolved, but the other aren't?


Solution

  • You didn't specify what the parameter A is supposed to be, so the compiler cannot even start searching for the implicit evidence, because it does not know the type of the thing it's supposed to find.

    This here behaves completely as expected:

    type PartiallyAppliedEither[A] = Either[String, A]
    def checkTypeEquality1(implicit ev: PartiallyAppliedEither[_] =:= Either[String, _]) = 1
    checkTypeEquality1      // compiles
    def checkTypeEquality2[A](implicit ev: PartiallyAppliedEither[A] =:= Either[String, A]) = 1 
    checkTypeEquality2[Int] // compiles
    def checkTypeEquality3[A](implicit ev: PartiallyAppliedEither[A] =:= Either[String, _]) = 1
    checkTypeEquality3[Int] // doesn't compile, because it shouldn't.
    

    The last one does not compile, because if it did, it would mean that every type is equal to any other type, but that's not the case.