Skip to content

Suspect inference incorrectly fails with type parameter #11056

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 Jan 11, 2021 · 1 comment
Closed

Suspect inference incorrectly fails with type parameter #11056

hmf opened this issue Jan 11, 2021 · 1 comment

Comments

@hmf
Copy link

hmf commented Jan 11, 2021

Minimized code

  object data {

    trait OfType[T]
    case object IntT extends OfType[Int]
    case object DoubleT extends OfType[Double]
    case object FloatT extends OfType[Float]

    type DSeq[X] = scala.collection.immutable.AbstractSeq[X]

    case class ColumnName[T](n:String, t: OfType[T])
    case class Column[T,F[_]<:DSeq[_]](n:F[T], of: ColumnName[T])
  }

  def min[T,F[_]<:data.DSeq[_]](col: data.Column[T,F])(using o:Ordering[T]): T = {
    col.n.min(o)
  }

  def min0[T,F[_]<:data.DSeq[T]](col: data.Column[T,F])(using o:Ordering[T]): T = {
    col.n.min[T](o)
  }

  def min1[T,F[_]<:data.DSeq[T]](col: data.Column[T,F])(using o:Ordering[T]): T = {
    col match {
      case data.Column(n:F[T],data.ColumnName(_,data.IntT)) => n.min[T](o)
      case data.Column(n,data.ColumnName(_,data.DoubleT)) => ???
      case data.Column(n,data.ColumnName(_,data.FloatT)) => ???

    }
  }

  def min2[T,F[_]<:data.DSeq[T]](col: data.Column[T,F])(using o:Ordering[T]): T = {
    col match {
      case data.Column(n:F[T],data.ColumnName(_,data.IntT)) => n.min[T](Ordering[Int])
      case data.Column(n,data.ColumnName(_,data.DoubleT)) => ???
      case data.Column(n,data.ColumnName(_,data.FloatT)) => ???

    }
  }

  def min3[T,F[X]<:data.DSeq[X]](col: data.Column[T,F])(using o:Ordering[T]): T = {
    col match {
      case data.Column(n:F[T],data.ColumnName(_,data.IntT)) => n.min[T](Ordering[Int])
      case data.Column(n,data.ColumnName(_,data.DoubleT)) => ???
      case data.Column(n,data.ColumnName(_,data.FloatT)) => ???

    }
  }

Output

I get in:

  def min[T,F[_]<:data.DSeq[_]](col: data.Column[T,F])(using o:Ordering[T]): T = {
    col.n.min[T](o)
  }

the error:

[error] 25 |    col.n.min(o)
[error]    |              ^
[error]    |              Found:    (o : Ordering[T])
[error]    |              Required: Ordering[B]
[error]    |
[error]    |              where:    B is a type variable which is an alias of Any
[error] one error found

Even when I use:

  def min[T,F[_]<:data.DSeq[_]](col: data.Column[T,F])(using o:Ordering[T]): T = {
    col.n.min[T](o)
  }

I still get:

[error] 25 |    col.n.min[T](o)
[error]    |    ^^^^^^^^^^^^^^^
[error]    |    Found:    Any
[error]    |    Required: T
[error] one error found

Expectation

I expected min to compile without explicitly setting the DSeq type parameter. The same should be true of all other versions, but I have to use either F[_]<:data.DSeq[T] or even F[X]<:data.DSeq[X]. Maybe I am expecting to much from the inference system?

@odersky
Copy link
Contributor

odersky commented Jan 12, 2021

It has to be either F[_] <: data.DSeq[T] or F[X] <: data.DSeq[X]. Both work. DSeq[_] is simply the wrong bound. Type inference cannot fix this.

@odersky odersky closed this as completed Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants