Skip to content

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

Closed
@weihsiu

Description

@weihsiu

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions