Skip to content

Instantly share code, notes, and snippets.

@keynmol
Created March 20, 2024 08:57
Show Gist options
  • Save keynmol/e1c6549bf366e37138aa9ffdcd52e2ac to your computer and use it in GitHub Desktop.
Save keynmol/e1c6549bf366e37138aa9ffdcd52e2ac to your computer and use it in GitHub Desktop.
TODO macro in Scala 3
def myLazyWork(str: String) =
str match
case "hello" => println("yo")
case "world" => TODO("world is unhandled")
@main def hello =
myLazyWork("hello")
myLazyWork("world")
// Inspired by this tweet: https://twitter.com/awesomekling/status/1770369132141121840
import scala.quoted.*
transparent inline def TODO(inline msg: String) =
${ todoImpl('msg) }
private class You_fucked_around_and_found_out(msg: String)
extends RuntimeException(msg)
private def todoImpl(msg: Expr[String])(using Quotes): Expr[Any] =
val value = msg.valueOrAbort
quotes.reflect.report.warning(s"TODO: $value")
'{ throw new You_fucked_around_and_found_out($msg) }
@keynmol
Copy link
Author

keynmol commented Mar 20, 2024

CleanShot 2024-03-20 at 08 50 21@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment