Skip to content

Compiler cannot unify types of a GADT #12390

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
regiskuckaertz opened this issue May 9, 2021 · 3 comments · Fixed by #12397
Closed

Compiler cannot unify types of a GADT #12390

regiskuckaertz opened this issue May 9, 2021 · 3 comments · Fixed by #12397
Assignees
Milestone

Comments

@regiskuckaertz
Copy link

Compiler version

3.0.0-RC3

Minimized code

enum Func[-A, +B] {
  case Double extends Func[Int, Int]
  case ToString extends Func[Float, String]

  def run: A => B = this match {
    case Double => (x: Int) => x * 2
    case ToString => (x: Float) => x.toString
  }
}

Output

// The compiler complains at each case:
Found: Int
Required: B
// and
Fount: String
Required: B

Expectation

The compiler should be able to unify B with Int or String depending on the case being evaluated.

@LPTK
Copy link
Contributor

LPTK commented May 10, 2021

Workaround:

enum Func[-A, +B] {
  case Double() extends Func[Int, Int]
  case ToString() extends Func[Float, String]

  def run: A => B = this match {
    case Double() => (x: Int) => x * 2
    case ToString() => (x: Float) => x.toString
  }
}

Currently, GADTs do not do any reasoning about singletons, which is a big limitation. I believe @Linyxus is already working on fixing that.

@regiskuckaertz
Copy link
Author

Oh nice! Thank you, that works for me. I'll keep this one opened for tracking.

@regiskuckaertz
Copy link
Author

@abgruszecki 🙇

@Kordyjan Kordyjan added this to the 3.0.1 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.

5 participants