-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #20287: Add flexible types to Quotes library #20293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Inlined(None, Nil, Literal(StringConstant("hello"))) | ||
ConstantType(StringConstant("hello")) | ||
|
||
Inlined(None, Nil, Apply(Select(Literal(StringConstant("world")), "trim"), Nil)) | ||
FlexibleType(TypeRef(ThisType(TypeRef(NoPrefix(), "lang")), "String")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmmmh, I'm wondering why we don't have the We do have it for the second type below 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess because it is from a Java method? |
||
|
||
FlexibleType(TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "java")), "lang"), "String")) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import scala.quoted.* | ||
|
||
object Macros { | ||
|
||
inline def printTree[T](inline x: T): Unit = | ||
${ impl('x) } | ||
|
||
def impl[T](x: Expr[T])(using Quotes): Expr[Unit] = { | ||
import quotes.reflect.* | ||
|
||
val tree = x.asTerm | ||
val treeStr = Expr(tree.show(using Printer.TreeStructure)) | ||
val treeTpeStr = Expr(tree.tpe.show(using Printer.TypeReprStructure)) | ||
|
||
'{ | ||
println(${treeStr}) | ||
println(${treeTpeStr}) | ||
println() | ||
} | ||
} | ||
|
||
inline def theTestBlock: Unit = ${ theTestBlockImpl } | ||
|
||
def theTestBlockImpl(using Quotes): Expr[Unit] = { | ||
import quotes.reflect.* | ||
|
||
val ft1 = FlexibleType(TypeRepr.of[String]) | ||
val ft1e = Expr(ft1.show(using Printer.TypeReprStructure)) | ||
|
||
'{ | ||
println(${ft1e}) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
import Macros.* | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
printTree("hello") | ||
printTree("world".trim()) | ||
theTestBlock | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.