-
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.
Allow to add type parameters to newClass
- Loading branch information
Showing
6 changed files
with
214 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Test_2$package$foo$1 | ||
{ | ||
class foo[A, B <: scala.Int](val param1: A, val param2: B) extends java.lang.Object { | ||
type A | ||
type B <: scala.Int | ||
} | ||
|
||
(new foo[java.lang.String, scala.Int]("test", 1): 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,45 @@ | ||
//> using options -experimental | ||
|
||
import scala.quoted.* | ||
|
||
transparent inline def makeClass(inline name: String): Any = ${ makeClassExpr('name) } | ||
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Any] = { | ||
import quotes.reflect.* | ||
|
||
val name = nameExpr.valueOrAbort | ||
def decls(cls: Symbol): List[Symbol] = Nil | ||
val constrType = | ||
(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 cls = Symbol.newClass( | ||
Symbol.spliceOwner, | ||
name, | ||
parents = _ => List(TypeRepr.of[Object]), | ||
decls, | ||
selfType = None, | ||
constrType, | ||
Flags.EmptyFlags, | ||
Symbol.noSymbol, | ||
Flags.EmptyFlags, | ||
Symbol.noSymbol, | ||
List(List(Flags.EmptyFlags, Flags.EmptyFlags), List(Flags.EmptyFlags, Flags.EmptyFlags)) | ||
) | ||
|
||
val clsDef = ClassDef(cls, List(TypeTree.of[Object]), body = Nil) | ||
val newCls = | ||
cls.typeRef.asType match | ||
case '[t] => | ||
Typed(Apply(TypeApply(Select(New(TypeIdent(cls)), cls.primaryConstructor), List(TypeTree.of[String], TypeTree.of[Int])), List(Expr("test").asTerm, Expr(1).asTerm)), TypeTree.of[Any]) | ||
|
||
val res = Block(List(clsDef), newCls).asExpr | ||
|
||
Expr.ofTuple(res, Expr(res.show)) | ||
|
||
// '{ | ||
// class `name`[A, B <: Int](param1: A, param2: B) | ||
// new `name`[String, Int]("a", 1) | ||
// } | ||
} |
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,7 @@ | ||
//> using options -experimental | ||
|
||
@main def Test: Unit = { | ||
val (cls, show) = makeClass("foo") | ||
println(cls.getClass) | ||
println(show) | ||
} |