Skip to content

Problem with type inference when using Iterator.sliding on Union types #8963

Closed
@hmf

Description

@hmf

Minimized code

  type Numeric = Double | Int

  sealed trait VarValues[T, C <: VarValues[T,C]] {
    this: C =>
    val arr: Array[T]
  }
  final case class VarDoubles(arr: Array[Double]) extends VarValues[Double, VarDoubles]
  final case class VarInts(arr: Array[Int]) extends VarValues[Int, VarInts]
  final case class VarStrs(arr: Array[String]) extends VarValues[String, VarStrs]

  def check7(a: VarValues[_,_], b: VarValues[_,_]): Unit = {
    (a,b) match {
      case (x:(VarDoubles|VarInts), y:(VarDoubles|VarInts)) =>
        val x0: Iterator[Numeric] = x.arr.iterator
        val y0: Iterator[Numeric] = y.arr.iterator
        val l0: Iterator[(Numeric, Numeric)] = x0.zip(y0)
        val ll0: Iterator[(Numeric, Numeric)]#GroupedIterator[(Numeric, Numeric)] = x0.zip(y0).sliding(2,1)
        ???
      case _ => ???
    }

Output

The line:

        val ll0: Iterator[(Numeric, Numeric)]#GroupedIterator[(Numeric, Numeric)] = x0.zip(y0).sliding(2,1)

generates the error:

[error] -- [E007] Type Mismatch Error: /home/hmf/IdeaProjects/snol/splotly/src/gg/SlidingIssue.scala:115:102 
[error] 115 |        val ll0: Iterator[(Numeric, Numeric)]#GroupedIterator[(Numeric, Numeric)] = x0.zip(y0).sliding(2,1)
[error]     |                                                                                    ^^^^^^^^^^^^^^^^^^^^^^^
[error]     |Found:    Iterator[(gg.SlidingIssue.Numeric, AnyVal)]#GroupedIterator[(
[error]     |  gg.SlidingIssue.Numeric
[error]     |, AnyVal)]
[error]     |Required: Iterator[(gg.SlidingIssue.Numeric, gg.SlidingIssue.Numeric)]#GroupedIterator[(
[error]     |  gg.SlidingIssue.Numeric
[error]     |, gg.SlidingIssue.Numeric)]
[error] one error found

Expectation

Not an expert so may be wrong, but ....
I expected val ll0 to contain a tuple of the same type (Numeric) however the second element of the tuple is inferred to be AnyVal. This may be related to #8956 but it looks like type projection may also be causing problems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions