Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Philipp Martini
committed
Dec 6, 2020
1 parent
fe499bf
commit fdb313e
Showing
12 changed files
with
267 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ | |
**/#* | ||
**/.#* | ||
tmp/ | ||
project | ||
target | ||
.bsp | ||
.metals/ | ||
|
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 @@ | ||
sbt.version=1.4.4 |
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 @@ | ||
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.6") |
File renamed without changes.
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,12 @@ | ||
package magnolia | ||
|
||
import scala.quoted._ | ||
|
||
object IsObject { | ||
inline def apply[T]: Boolean = ${ isObjectImpl[T] } | ||
|
||
def isObjectImpl[T](using qctx: Quotes, tpe: Type[T]): Expr[Boolean] = { | ||
import qctx.reflect._ | ||
Expr(TypeRepr.of[T].typeSymbol.flags.is(Flags.Object)) | ||
} | ||
} |
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,27 @@ | ||
package magnolia | ||
|
||
import scala.quoted._ | ||
|
||
object ParamAnnotations { | ||
inline def apply[T]: List[(String, List[Any])] = ${ paramAnnotationsImpl[T] } | ||
|
||
def paramAnnotationsImpl[T](using qctx: Quotes, tpe: Type[T]): Expr[List[(String, List[Any])]] = { | ||
import qctx.reflect._ | ||
|
||
val tpe = TypeRepr.of[T] | ||
|
||
Expr.ofList( | ||
tpe | ||
.typeSymbol | ||
.caseFields | ||
.map(field => | ||
Expr(field.name) -> field.annots.filter(a => | ||
a.tpe.typeSymbol.maybeOwner.isNoSymbol || | ||
a.tpe.typeSymbol.owner.fullName != "scala.annotation.internal" | ||
).map(_.asExpr.asInstanceOf[Expr[Any]]) | ||
) | ||
.filter(_._2.nonEmpty) | ||
.map{ case (name, annots) => Expr.ofTuple(name, Expr.ofList(annots)) } | ||
) | ||
} | ||
} |
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,40 @@ | ||
package magnolia | ||
|
||
object TestApp extends App { | ||
case class MyAnnotation(a: Int) extends scala.annotation.Annotation | ||
|
||
@MyAnnotation(1) case class MyCaseClass[A](@MyAnnotation(2) @MyAnnotation(10) i: A @MyAnnotation(3) ,@MyAnnotation(4) s: String @MyAnnotation(5)) | ||
|
||
trait Show[T] { | ||
def show(t: T): String | ||
} | ||
|
||
object Show extends MagnoliaDerivation[Show] { | ||
def combine[T](ctx: CaseClass[Show, T]): Show[T] = new Show[T] { | ||
def show(value: T): String = ctx.parameters.map { p => | ||
s"${p.label}=${p.typeclass.show(p.dereference(value))}" | ||
}.mkString("{", ",", "}") | ||
} | ||
|
||
def dispatch[T](ctx: SealedTrait[Show, T]): Show[T] = { | ||
new Show[T] { | ||
def show(value: T): String = ctx.dispatch(value) { sub => | ||
sub.typeclass.show(sub.cast(value)) | ||
} | ||
} | ||
} | ||
|
||
given IntShow as Show[Int] { | ||
def show(t: Int): String = t.toString | ||
} | ||
|
||
given StringShow as Show[String] { | ||
def show(t: String): String = t | ||
} | ||
} | ||
|
||
List( | ||
MyCaseClass[Int](1, "a") | ||
).map(Show.derived[MyCaseClass[Int]].show).foreach(println) | ||
|
||
} |
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,22 @@ | ||
package magnolia | ||
|
||
import scala.quoted._ | ||
|
||
object TypeAnnotations { | ||
inline def apply[T]: List[Any] = ${ typeAnnotationsImpl[T] } | ||
|
||
def typeAnnotationsImpl[T](using qctx: Quotes, tpe: Type[T]): Expr[List[Any]] = { | ||
import qctx.reflect._ | ||
|
||
Expr.ofList( | ||
TypeRepr | ||
.of[T] | ||
.typeSymbol | ||
.annots | ||
.filter(a => | ||
a.tpe.typeSymbol.maybeOwner.isNoSymbol || | ||
a.tpe.typeSymbol.owner.fullName != "scala.annotation.internal" | ||
).map(_.asExpr.asInstanceOf[Expr[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,49 @@ | ||
package magnolia | ||
|
||
import scala.quoted._ | ||
|
||
sealed trait TypeInfo { | ||
def full: String | ||
} | ||
|
||
case class BaseTypeInfo(owner: String, name: String, typeParams: List[TypeInfo]) extends TypeInfo { | ||
override def full: String = "" // TODO | ||
} | ||
case class OrTypeInfo(subtypes: List[TypeInfo]) extends TypeInfo { | ||
override def full: String = "" // TODO | ||
} | ||
case class AndTypeInfo(subtypes: List[TypeInfo]) extends TypeInfo { | ||
override def full: String = "" // TODO | ||
} | ||
|
||
object TypeInfo { | ||
inline def apply[T]: TypeInfo = ${ typeInfoImpl[T] } | ||
|
||
def typeInfoImpl[T](using qctx: Quotes, tpe: Type[T]): Expr[TypeInfo] = { | ||
import qctx.reflect._ | ||
|
||
def name(tpe: TypeRepr) : Expr[String] = | ||
Expr(tpe.typeSymbol.name) | ||
|
||
def owner(tpe: TypeRepr): Expr[String] = | ||
if (tpe.typeSymbol.maybeOwner.isNoSymbol) { | ||
println("Debug: No owner - " + tpe) // TODO - remove | ||
Expr("<no owner>") // TODO: can this happen any more? are all cases catched? how to deal with unhandled cases? | ||
} else if (tpe.typeSymbol.owner == defn.EmptyPackageClass) Expr("") | ||
else Expr(tpe.typeSymbol.owner.fullName) | ||
|
||
def typeInfo(tpe: TypeRepr): Expr[TypeInfo] = | ||
tpe match { | ||
case OrType(subtypes) => | ||
'{OrTypeInfo(${Expr.ofList(subtypes.toList.map(typeInfo))})} | ||
case AndType(subtypes) => | ||
'{AndTypeInfo(${Expr.ofList(subtypes.toList.map(typeInfo))})} | ||
case AppliedType(tpe, args) => | ||
'{BaseTypeInfo(${owner(tpe)}, ${name(tpe)}, ${Expr.ofList(args.map(typeInfo))})} | ||
case _ => | ||
'{BaseTypeInfo(${owner(tpe)}, ${name(tpe)}, List.empty)} | ||
} | ||
|
||
typeInfo(TypeRepr.of[T]) | ||
} | ||
} |
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
Oops, something went wrong.