Skip to content

Instantly share code, notes, and snippets.

@hejfelix
Created February 20, 2023 17:59
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 hejfelix/24a59869231cf435a03370bd03d361ec to your computer and use it in GitHub Desktop.
Save hejfelix/24a59869231cf435a03370bd03d361ec to your computer and use it in GitHub Desktop.
val scala3Version = "3.2.0"
lazy val root = project
.in(file("."))
.settings(
name := "macro-repro",
version := "0.1.0-SNAPSHOT",
scalaVersion := scala3Version,
)
class Foo[F[_]] {
}
object Main:
def main(args: Array[String]):Unit = println(SourceMacro.getContent[Foo[?]])
end Main
object SourceMacro {
import scala.quoted.*
inline def getContent[A]: String = ${getContentImpl[A]}
def getContentImpl[A: Type](using Quotes): Expr[String] =
import quotes.reflect.*
// for the source specifically of A
// val str = TypeRepr.of[A].typeSymbol.tree.pos.sourceCode.getOrElse(
// report.errorAndAbort("no source code")
// )
// for the whole source file containing the definition of A
val str = TypeRepr.of[A].typeSymbol.pos.getOrElse(
report.errorAndAbort("no symbol position")
).sourceFile.content.getOrElse(
report.errorAndAbort("no source-file content")
)
Expr(str)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment