Skip to content

Recursive extension method using implicit argument with generic fails to infer correct types #9509

Closed
@micheal-hill

Description

@micheal-hill

Minimized code

enum AList[+A] {
  case Cons(head: A, tail: AList[A])
  case Nil
}

object AList {
  extension [A](l: AList[A]) def sum(using numeric: Numeric[A]): A = l match {
    case Cons(x, xs) => numeric.plus(x, xs.sum(using numeric))
    case Nil         => numeric.zero
  }
}

Output

Compiler error at line 10:

Found:    (numeric : Numeric[A])
Required: Numeric[A$1]

where:    A   is a type in method extension_sum with bounds >: A$1
          A$1 is a type in method extension_sum with bounds <: A

Dropping the explicit parameter list when calling sum recursively results in the error:

value sum is not a member of AList[A$1].
An extension method was tried, but could not be fully constructed:

    AList.extension_sum[A$1](xs)(numeric)

where:    A$1 is a type in method extension_sum with bounds <: A

Scastie repro to demonstrate both.

Expectation

Expect that this should compile and allow (explicitly or not) the given numeric instance to be used for the recursive call.
From chat with @smarter here:

yeah type inference is being too eager and instantiating the type parameter of sum before typing the second argument
so it ends up choosing the pattern-bound A$1 instead of A

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions