Skip to content

Match type not working unless ascribed or inlined (2) #11247

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
nicolasstucki opened this issue Jan 29, 2021 · 3 comments · Fixed by #11263
Closed

Match type not working unless ascribed or inlined (2) #11247

nicolasstucki opened this issue Jan 29, 2021 · 3 comments · Fixed by #11263

Comments

@nicolasstucki
Copy link
Contributor

Minimized code

import annotation.showAsInfix
import scala.compiletime._

class TupleKOps {


  def tupleMerge(tuple1: TupleK, tuple2: TupleK): TupleK = ???

  def tupleMergeSort(tuple: TupleK): TupleK =
    if (tuple.size <= 1) tuple
    else {
      val (tuple1, tuple2) = tuple.splitAt(tuple.size / 2)
      // val (tuple1: TupleK, tuple2: TupleK) = tuple.splitAt(tuple.size / 2) // ok
      val (sorted1, sorted2) = (tupleMergeSort(tuple1), tupleMergeSort(tuple2))
      tupleMerge(sorted1, sorted2)
    }

}

sealed trait TupleK {
  import TupleK._
  /*inline*/ def size[This >: this.type <: TupleK]: Size[This] = ???
  /*inline*/ def splitAt[This >: this.type <: TupleK](n: Int): Split[This, n.type] = ???
}

object TupleK {
  type Size[X <: TupleK] <: Int = X match {
    case EmptyTupleK => 0
    case x #: xs => S[Size[xs]]
  }
  type Take[T <: TupleK, N <: Int] <: TupleK = N match {
    case 0 => EmptyTupleK
    case S[n1] => T match {
      case EmptyTupleK => EmptyTupleK
      case x #: xs => x #: Take[xs, n1]
    }
  }
  type Drop[T <: TupleK, N <: Int] <: TupleK = N match {
    case 0 => T
    case S[n1] => T match {
      case EmptyTupleK => EmptyTupleK
      case x #: xs => Drop[xs, n1]
    }
  }
  type Split[T <: TupleK, N <: Int] = (Take[T, N], Drop[T, N])
}

type EmptyTupleK = EmptyTupleK.type

object EmptyTupleK extends TupleK

sealed trait NonEmptyTupleK extends TupleK
sealed abstract class #:[+H, +T <: TupleK] extends NonEmptyTupleK

Output

-- [E043] Type Error: tests/pos/tuples1c.scala:12:6 ----------------------------
12 |      val (tuple1, tuple2) = tuple.splitAt(tuple.size / 2)
   |      ^
   |unreducible application of higher-kinded type TupleK.Take to wildcard arguments in inferred type TupleK.Take[TupleK, ? <: Int]

Expectation

They compile with the inline, they should also compile without it.

@nicolasstucki
Copy link
Contributor Author

Related to #11236

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 29, 2021
@odersky
Copy link
Contributor

odersky commented Jan 29, 2021

It seems here the problem is that tuple.size is not constant unless the inline is presented? The workaround dotty-staging@ef72a88 sidesteps the problem by widening the type. We could do the same automatically, maybe, but in most cases it would simply render the error more obscure.

@odersky
Copy link
Contributor

odersky commented Jan 31, 2021

On second thought, I think it's OK to make an exception for match types here. It's expected that some match types don't reduce because the arguments are not sufficiently well known. In that case we should widen to the bound instead of failing with an error.

odersky added a commit to dotty-staging/dotty that referenced this issue Jan 31, 2021
nicolasstucki pushed a commit to dotty-staging/dotty that referenced this issue Feb 1, 2021
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Feb 1, 2021
nicolasstucki pushed a commit to dotty-staging/dotty that referenced this issue Feb 1, 2021
nicolasstucki pushed a commit to dotty-staging/dotty that referenced this issue Feb 1, 2021
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Feb 2, 2021
nicolasstucki pushed a commit to dotty-staging/dotty that referenced this issue Feb 2, 2021
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Feb 2, 2021
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Feb 4, 2021
michelou pushed a commit to michelou/scala3 that referenced this issue Feb 5, 2021
@Kordyjan Kordyjan added this to the 3.0.0 milestone Aug 2, 2023
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.

3 participants