-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix method signatures and add test for abstract classes and traits
- Loading branch information
Showing
10 changed files
with
174 additions
and
55 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Test_2$package$anon$1 | ||
{ | ||
trait foo[A, B <: scala.Int](val param1: A, val param2: B) extends java.lang.Object | ||
class anon() extends java.lang.Object with foo[java.lang.String, scala.Int]("a", 1) | ||
|
||
(new anon(): scala.Any) | ||
} | ||
class Test_2$package$anon$2 | ||
{ | ||
abstract class bar[A, B <: scala.Int](val param1: A, val param2: B) extends java.lang.Object | ||
class anon() extends bar[java.lang.String, scala.Int]("a", 1) | ||
|
||
(new anon(): scala.Any) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//> using options -experimental | ||
|
||
import scala.quoted.* | ||
|
||
transparent inline def makeTrait(inline name: String): Any = ${ makeTraitExpr('name) } | ||
transparent inline def makeAbstractClass(inline name: String): Any = ${ makeAbstractClassExpr('name) } | ||
|
||
private def makeTraitExpr(name: Expr[String])(using Quotes): Expr[Any] = { | ||
makeClassExpr(name, quotes.reflect.Flags.Trait, List(quotes.reflect.TypeTree.of[Object])) | ||
// '{ | ||
// trait `name`[A, B <: Int](param1: A, param2: B) | ||
// class anon() extends `name`[String, Int]("a", 1) | ||
// new $anon() | ||
// } | ||
} | ||
|
||
private def makeAbstractClassExpr(name: Expr[String])(using Quotes): Expr[Any] = { | ||
makeClassExpr(name, quotes.reflect.Flags.Abstract, Nil) | ||
// '{ | ||
// abstract class `name`[A, B <: Int](param1: A, param2: B) | ||
// class anon() extends `name`[String, Int]("a", 1) | ||
// new $anon() | ||
// } | ||
} | ||
|
||
private def makeClassExpr(using Quotes)( | ||
nameExpr: Expr[String], clsFlags: quotes.reflect.Flags, childExtraParents: List[quotes.reflect.TypeTree] | ||
): Expr[Any] = { | ||
import quotes.reflect.* | ||
|
||
val name = nameExpr.valueOrAbort | ||
def decls(cls: Symbol): List[Symbol] = Nil | ||
val conMethodType = | ||
(classType: TypeRepr) => PolyType(List("A", "B"))( | ||
_ => List(TypeBounds.empty, TypeBounds.upper(TypeRepr.of[Int])), | ||
polyType => MethodType(List("param1", "param2"))((_: MethodType) => List(polyType.param(0), polyType.param(1)), (_: MethodType) => classType) | ||
) | ||
|
||
val traitSymbol = Symbol.newClass( | ||
Symbol.spliceOwner, | ||
name, | ||
parents = _ => List(TypeRepr.of[Object]), | ||
decls, | ||
selfType = None, | ||
clsFlags, | ||
clsPrivateWithin = Symbol.noSymbol, | ||
conMethodType, | ||
conFlags = Flags.EmptyFlags, | ||
conPrivateWithin = Symbol.noSymbol, | ||
conParamFlags = List(List(Flags.EmptyFlags, Flags.EmptyFlags), List(Flags.EmptyFlags, Flags.EmptyFlags)) | ||
) | ||
val traitDef = ClassDef(traitSymbol, List(TypeTree.of[Object]), body = Nil) | ||
|
||
val traitTypeTree = Applied(TypeIdent(traitSymbol), List(TypeTree.of[String], TypeTree.of[Int])) | ||
val clsSymbol = Symbol.newClass( | ||
Symbol.spliceOwner, | ||
"anon", | ||
parents = _ => childExtraParents.map(_.tpe) ++ List(traitTypeTree.tpe), | ||
decls = _ => Nil, | ||
selfType = None, | ||
clsFlags = Flags.EmptyFlags, | ||
clsPrivateWithin = Symbol.noSymbol, | ||
conMethodType = (classType: TypeRepr) => MethodType(Nil)(_ => Nil, _ => classType), | ||
conFlags = Flags.EmptyFlags, | ||
conPrivateWithin = Symbol.noSymbol, | ||
conParamFlags = List(List()) | ||
) | ||
val obj = '{new java.lang.Object()}.asTerm match | ||
case Inlined(_, _, term) => term | ||
|
||
val parentsWithSym = childExtraParents ++ List( | ||
Apply( | ||
TypeApply( | ||
Select(New(traitTypeTree), traitSymbol.primaryConstructor), | ||
List(TypeTree.of[String], TypeTree.of[Int]) | ||
), | ||
List(Expr("a").asTerm, Expr(1).asTerm) | ||
) | ||
) | ||
val clsDef = ClassDef(clsSymbol, parentsWithSym, body = Nil) | ||
|
||
val newCls = | ||
Typed( | ||
Apply( | ||
Select(New(TypeIdent(clsSymbol)), clsSymbol.primaryConstructor), | ||
Nil | ||
), | ||
TypeTree.of[Any] | ||
) | ||
val res = Block(List(traitDef), Block(List(clsDef), newCls)).asExpr | ||
|
||
Expr.ofTuple(res, Expr(res.show)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//> using options -experimental | ||
|
||
@main def Test: Unit = { | ||
val (cls1, show1) = makeTrait("foo") | ||
println(cls1.getClass) | ||
println(show1) | ||
|
||
val (cls2, show2) = makeAbstractClass("bar") | ||
println(cls2.getClass) | ||
println(show2) | ||
} |
Oops, something went wrong.