Created
October 15, 2023 21:40
-
-
Save kubukoz/ddafe01b9a7ff4448bebb701356d7f56 to your computer and use it in GitHub Desktop.
Scala 3 inlines before tailrec analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compiles | |
//> using scala "3.3.1" | |
import scala.annotation.tailrec | |
case class Opt[A](value: A | Null) { | |
inline def flatMap[B](inline f: A => Opt[B]): Opt[B] = | |
if (value == null) | |
Opt(null) | |
else | |
f(value.asInstanceOf[A]) | |
} | |
@tailrec | |
final def unwrap(s: Int, calls: Int): Opt[Int] = | |
if calls > 0 then Opt(42).flatMap(unwrap(_, calls - 1)) | |
else Opt(42) | |
// doesn't compile | |
//> using scala "3.3.1" | |
import scala.annotation.tailrec | |
case class Opt[A](value: A | Null) { | |
def flatMap[B](f: A => Opt[B]): Opt[B] = | |
if (value == null) | |
Opt(null) | |
else | |
f(value.asInstanceOf[A]) | |
} | |
@tailrec | |
final def unwrap(s: Int, calls: Int): Opt[Int] = | |
if calls > 0 then Opt(42).flatMap(unwrap(_, calls - 1)) | |
else Opt(42) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment