-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add reflect ClassDef.apply
and Symbol.newClass
#14124
Add reflect ClassDef.apply
and Symbol.newClass
#14124
Conversation
8bc0c20
to
1fbda34
Compare
647629e
to
24586e1
Compare
7def96e
to
d32d4af
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM.
ClassDef.apply
and Symbol.newClass
2254fa4
to
4845353
Compare
|
||
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = Nil, decls, selfInfo = None) | ||
val clsDef = ClassDef(cls, parents, body = List()) | ||
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Object]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is orthogonal but in tpd we have a New overload that takes constructor arguments this could be useful here too
072c154
to
4800912
Compare
* @pre symbol.isType returns true | ||
*/ | ||
@experimental | ||
def typeRef: TypeRef |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that we're missing an API to create a TypeRef/TermRef from a specific prefix: with foo.typeRef
I can get a TypeRef this.foo
and with pre.memberType(foo)
I can get the info corresponding to pre.foo
, but is there a way to generate the TypeRef pre.foo
itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't TypeRepr.select
provide that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh indeed, I missed that one, maybe each of their documentation should @see
the other one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
*/ | ||
def makeFooBar(using Quotes)(name: String, fooCls: quotes.reflect.Symbol): quotes.reflect.ClassDef = { | ||
import quotes.reflect.* | ||
val parents = List(Inferred(fooCls.typeRef), TypeTree.of[Bar]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps instead of the Inferred(foo.typeRef)
pattern we could have an overload of TypeTree.apply (or TypeTree.of? Not sure why we use of
there) that takes a Symbol. This would be similar to the compiler-internal tpd.ref
(in fact alternatively we could expose ref
directly?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added Added TypeTree.apply
TypeTree.ref
.
We should probably add TypeTree.apply
as an alias of Inferred.apply
. We cannot overload this method.
d0bb8b6
to
cf0b09e
Compare
a0d7004
to
26bb201
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM!
26bb201
to
36853ff
Compare
@nicolasstucki thank you for this!!! I am noticing a class cast exception if I try pattern matching
e.g. if I try to check the type of a case class field like: // scala 3.2.0-RC2
declaredField.companionClass.caseFields.map { ccField =>
val tt = ccField.typeRef match {
case TypeRef(typeRepr, args) => s"args are: ${args}"
}
} I wasn't sure if this was a separate bug or not, happy to open a new one. It does seem odd seeing that exception; feels like a class cast exception happening internally so thought it might be important. Update: I've found this and while I'm still not sure about that class cast exception above, it does seem to be the case that matching on |
We also add
SymbolMethods.typeRef
SymbolMethods.termRef
TypeTreeModule.ref