Skip to content

Cannot reduce inline match with an enum-based Nat #6781

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
biboudis opened this issue Jul 1, 2019 · 5 comments · Fixed by #11317
Closed

Cannot reduce inline match with an enum-based Nat #6781

biboudis opened this issue Jul 1, 2019 · 5 comments · Fixed by #11317

Comments

@biboudis
Copy link
Contributor

biboudis commented Jul 1, 2019

minimized code

  enum Nat {
    case Zero
    case Succ[N <: Nat](n: N)
  }
  import Nat._

  inline def toInt(n: => Nat): Int = inline n match {
    case Zero => 0
    case Succ(n1) => toInt(n1) + 1
  }

  val natTwo = toInt(Succ(Succ(Zero)))

results in a compile time error:

inline n match {
   |                                     ^
   |                                     cannot reduce delegate match with
   |                                      scrutinee:  {
   |                                       App.Nat.Succ.apply[App.Nat](App.Nat.Succ.apply[App.Nat](App.Nat$#Zero))
   |                                     } : App.Nat
   |                                      patterns :  case App.Nat$#Zero
   |                                                  case App.Nat.Succ.unapply[N$1 @ N$1](n1 @ _):App.Nat.Succ[N$1]

expectation

To work as the "old" encoding:

trait Nat
case object Zero extends Nat
case class Succ[N <: Nat](n: N) extends Nat 

nitpick: the error message should have been about inline match instead of delegate

@biboudis biboudis changed the title Cannot reduce inline match with an enum-based encoding of Nat Cannot reduce inline match with an enum-based Nat Jul 1, 2019
@nicolasstucki
Copy link
Contributor

@AleksanderBG you had some insight of how the N in case Succ[N <: Nat](n: N) is handled differently than in case class Succ[N <: Nat](n: N) extends Nat

@abgruszecki
Copy link
Contributor

@nicolasstucki inline match AFAIR reduces based on the type of the expression and Succ(x) in the example always returns a Nat, so this just isn't going to work. You can even see in the error message that the type of the scrutinee is App.Nat:

   cannot reduce delegate match with
     scrutinee:  {
       App.Nat.Succ.apply[App.Nat](App.Nat.Succ.apply[App.Nat](App.Nat$#Zero))
     } : App.Nat

@abgruszecki
Copy link
Contributor

Unassigning myself and removing the bug label, since this is more of a design limitation than a bug. We can talk about it during the next monday meeting.

@abgruszecki abgruszecki removed their assignment Jul 2, 2019
@biboudis
Copy link
Contributor Author

biboudis commented Jul 3, 2019

@odersky wdyt?

@odersky
Copy link
Contributor

odersky commented Jul 4, 2019

I agree it's a spec thing. One more fact that strengthens the case for making cases have the more specific type.

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