bug icon indicating copy to clipboard operation
bug copied to clipboard

Incomplete substitution of type parameters in extension methods

Open joroKr21 opened this issue 6 years ago • 5 comments

This occurs when combining AnyVal extension methods and the Aux pattern for dependent types.

trait DepFn[A] { type B; def apply(a: A): B }
object DepFn { type Aux[A, C] = DepFn[A] { type B = C } }
class Syntax(val i: Int) extends AnyVal {
  def foo[A](e: Either[Int => A, DepFn.Aux[Int, A]]) = e.fold(_(i), _(i))
}

// Exiting paste mode, now interpreting.

  def foo[A](e: Either[Int => A, DepFn.Aux[Int, A]]) = e.fold(_(i), _(i))
                                                                     ^
<pastie>:4: error: type mismatch;
 found   : A
 required: A(in method foo$extension)

Note that surprisingly this compiles (the only difference is a type ascription):

class Syntax(val i: Int) extends AnyVal {
  def foo[A](e: Either[Int => A, DepFn.Aux[Int, A]]) = e.fold(_(i), _(i): A)
}

// Exiting paste mode, now interpreting.

defined class Syntax

And now the punchline (try it with a List[A]):

class Syntax(val i: Int) extends AnyVal {
  def foo[A](e: Either[Int => List[A], DepFn.Aux[Int, List[A]]]) = e.fold(_(i), _(i))
}

// Exiting paste mode, now interpreting.

  def foo[A](e: Either[Int => List[A], DepFn.Aux[Int, List[A]]]) = e.fold(_(i), _(i))
                                                                                 ^
<pastie>:4: error: type mismatch;
 found   : List[A]
 required: List[A]

huh? At this point the compiler looks silly :laughing:

joroKr21 avatar Jan 29 '19 02:01 joroKr21