Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hmf opened this issue May 12, 2020 · 2 comments · Fixed by #11012
Closed

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

hmf opened this issue May 12, 2020 · 2 comments · Fixed by #11012

Comments

@hmf
Copy link

hmf commented May 12, 2020

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.

@hmf hmf added the itype:bug label May 12, 2020
@hmf hmf changed the title Probken with inferencewhen using Iterator.sliding on Union types Problem with inference when using Iterator.sliding on Union types May 12, 2020
@hmf hmf changed the title Problem with inference when using Iterator.sliding on Union types Problem with type inference when using Iterator.sliding on Union types May 12, 2020
@hmf
Copy link
Author

hmf commented May 12, 2020

Workaround:

  def check8(a: VarValues[_,_], b: VarValues[_,_]): Unit = {
    (a,b) match {
      case (x:(VarDoubles|VarInts), y:(VarDoubles|VarInts)) =>
        /*val x0: Iterator[Double|Int] = x.arr.iterator
        val y0: Iterator[Double|Int] = y.arr.iterator*/
        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)] = l0.sliding(2,1)
        ???
      case _ => ???
    }

@bishabosha
Copy link
Member

minimised to:

scala> type Numeric = Int | Double
// defined alias type Numeric = Int | Double

scala> val x1: Iterator[Numeric] = Array(1).iterator
val x1: Iterator[Numeric] = <iterator>

scala> val y1: Iterator[Numeric] = Array(1.0).iterator                                                               
val y1: Iterator[Numeric] = <iterator>

scala> x1.zip(y1).sliding(2,1)
val res0: Iterator[(Numeric, AnyVal)]#GroupedIterator[(Numeric, AnyVal)] = <iterator>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants