Skip to content

Scala 2.13 regression in for comprehension with wildcard syntax which mixes Unit and Nothing #11604

Closed
@oxbowlakes

Description

@oxbowlakes

Consider the following code:

Welcome to Scala 2.12.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_181).
Type in expressions for evaluation. Or try :help.

scala> trait X[A] { def map[B](f: A => B): X[B]; def flatMap[B](f: A => X[B]): X[B] }
defined trait X

scala> lazy val void: X[Unit] = ???
void: X[Unit] = <lazy>

scala> lazy val unreachable: X[Nothing] = ???
unreachable: X[Nothing] = <lazy>

scala> lazy val x: X[Nothing] = for (_ <- void; u <- unreachable) yield u
x: X[Nothing] = <lazy>

Then if we use scala 2.13:

Welcome to Scala 2.13.0 (OpenJDK 64-Bit Server VM, Java 1.8.0_181).
Type in expressions for evaluation. Or try :help.

scala> trait X[A] { def map[B](f: A => B): X[B]; def flatMap[B](f: A => X[B]): X[B] }
defined trait X

scala> lazy val void: X[Unit] = ???
void: X[Unit] = <lazy>

scala> lazy val unreachable: X[Nothing] = ???
unreachable: X[Nothing] = <lazy>

scala> lazy val x: X[Nothing] = for (_ <- void; u <- unreachable) yield u
                                       ^
       error: type mismatch;
        found   : Unit => X[Nothing]
        required: Unit => X[B]

This can be "fixed" by assigning a non-underscore identifier to the Unit result of void. That is:

scala> lazy val x: X[Nothing] = for (v <- void; u <- unreachable) yield u
x: X[Nothing] = <lazy>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions