Skip to content

Incomplete substitution of type parameters in extension methods #11383

Open
scala/scala
#9172
@joroKr21

Description

@joroKr21

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 😆

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)typervalueclass

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions