Skip to content

Instantly share code, notes, and snippets.

@kubukoz
Created October 15, 2023 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kubukoz/ddafe01b9a7ff4448bebb701356d7f56 to your computer and use it in GitHub Desktop.
Save kubukoz/ddafe01b9a7ff4448bebb701356d7f56 to your computer and use it in GitHub Desktop.
Scala 3 inlines before tailrec analysis
// 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