Add reflect ClassDef.apply and Symbol.newClass#14124
Add reflect ClassDef.apply and Symbol.newClass#14124nicolasstucki merged 1 commit intoscala:mainfrom
ClassDef.apply and Symbol.newClass#14124Conversation
8bc0c20 to
1fbda34
Compare
647629e to
24586e1
Compare
7def96e to
d32d4af
Compare
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.
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.
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.
Doesn't TypeRepr.select provide that?
There was a problem hiding this comment.
Oh indeed, I missed that one, maybe each of their documentation should @see the other one.
| */ | ||
| 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.
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.
Added Added TypeTree.applyTypeTree.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
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.typeRefSymbolMethods.termRefTypeTreeModule.ref