Created
February 20, 2023 17:59
-
-
Save hejfelix/24a59869231cf435a03370bd03d361ec to your computer and use it in GitHub Desktop.
This file contains 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
val scala3Version = "3.2.0" | |
lazy val root = project | |
.in(file(".")) | |
.settings( | |
name := "macro-repro", | |
version := "0.1.0-SNAPSHOT", | |
scalaVersion := scala3Version, | |
) |
This file contains 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
class Foo[F[_]] { | |
} |
This file contains 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
object Main: | |
def main(args: Array[String]):Unit = println(SourceMacro.getContent[Foo[?]]) | |
end Main |
This file contains 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
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