Skip to content

Problematic type inference in supercall when type parameter appears only in contravariant position #2218

Closed
@liufengyun

Description

@liufengyun

Following code compiles fine in Scalac, but fails to compile in Dotty, because the type parameter inferred is new DefaultRule[Nothing, Out, A, X](f) with Name.

trait Rule[In, Out, A, X]
trait Result[Out, A, X]
trait Name

class C {
  def ruleWithName[In, Out, A, X](_name: String, f: In => Result[Out, A, X]): Rule[In, Out, A, X] with Name =
    new DefaultRule(f) with Name

  class DefaultRule[In, Out, A, X](f: In => Result[Out, A, X]) extends Rule[In, Out, A, X]
}

If we remove the intersection type, the following version compiles fine in Dotty:

trait Rule[In, Out, A, X]
trait Result[Out, A, X]

class C {
  def ruleWithName[In, Out, A, X](_name: String, f: In => Result[Out, A, X]): Rule[In, Out, A, X] =
    new DefaultRule(f)

  class DefaultRule[In, Out, A, X](f: In => Result[Out, A, X]) extends Rule[In, Out, A, X]
}

Note that adding variance doesn't help, following version also fails:

trait Rule[-In, +Out, +A, +X]
trait Result[+Out, +A, +X]
trait Name

class C {
  def ruleWithName[In, Out, A, X](_name: String, f: In => Result[Out, A, X]): Rule[In, Out, A, X] with Name =
    new DefaultRule(f) with Name

  class DefaultRule[In, Out, A, X](f: In => Result[Out, A, X]) extends Rule[In, Out, A, X]
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions