Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: compilation error when declaring continuation of type Any => Nothing

This code gives compilation error:

import scala.util.continuations._

object CTest {
    def loop: Nothing = reset {
        shift {c: (Unit => Nothing) => c()}
        loop
    }

   def main(argv: Array[String]) {loop}
}

Error message:

    error: type mismatch;
 found   : ((Unit) => Nothing) => (Unit) => Nothing
 required: ((Unit) => B) => (Unit) => Nothing

But this code works as expected:

import scala.util.continuations._

object CTest {
    def loop: Nothing = reset {
        shift {c: (Unit => Any) => c.asInstanceOf[Unit => Nothing]()}
        loop
    }

   def main(argv: Array[String]) {loop}
}

The question is: why Scala compiler hates me continuations of type Any => Nothing?

like image 303
E. Verda Avatar asked Nov 25 '25 20:11

E. Verda


1 Answers

It compiles if I specify the type arguments:

shift[Unit, Nothing, Nothing] {c: (Unit => Nothing) => c()}

It looks to me like the compiler should infer that B is Nothing, but it doesn't.

like image 93
Alexey Romanov Avatar answered Nov 28 '25 13:11

Alexey Romanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!