Skip to content

Unable to write a PolyFunction lambda with a "match type" return type #10369

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
weihsiu opened this issue Nov 18, 2020 · 2 comments · Fixed by #17102
Closed

Unable to write a PolyFunction lambda with a "match type" return type #10369

weihsiu opened this issue Nov 18, 2020 · 2 comments · Fixed by #17102

Comments

@weihsiu
Copy link

weihsiu commented Nov 18, 2020

Minimized code

  type Upgrade[T] = T match
    case Int => Double
    case Char => String
    case Boolean => Boolean

  // compiles
  val upgrade: [t] => t => Upgrade[t] = new PolyFunction:
    def apply[T](x: T): Upgrade[T] = x match
      case x: Int => x.toDouble
      case x: Char => x.toString
      case x: Boolean => !x
  
  // does not compile
  val upgrade2: [t] => t => Upgrade[t] = [t] => (x: t) => x match
      case x: Int => x.toDouble
      case x: Char => x.toString
      case x: Boolean => !x

Output

[error] -- [E007] Type Mismatch Error: /.../Tuples.scala:42:27 e / compileIncremental 0s
[error] 42 |      case x: Boolean => !x
[error]    |                           ^
[error]    |       Found:    Object with PolyFunction {...}
[error]    |       Required: PolyFunction{apply: [t](x$1: t): tlp.Tuples.Upgrade[t]}

Expectation

it should compile.

btw, polyfunction lambda with "regular" return types works just fine.

@prolativ
Copy link
Contributor

This might be somehow related to #6904

@bishabosha
Copy link
Member

the issue appears to be that the right hand side of your polymorphic function lambda can't infer Upgrade[T]. adding an explicit ascription works:

val upgrade2 = [t] => (x: t) =>
  (x match
    case x: Int => x.toDouble
    case x: Char => x.toString
    case x: Boolean => !x): Upgrade[t]

Decel added a commit to Decel/dotty that referenced this issue Mar 15, 2023
Was fixed in `3.2.1-RC1`. Add a regression test for scala#10369.
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.

4 participants