-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Port gestalt TypeToolbox #5388
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
Merged
nicolasstucki
merged 2 commits into
scala:master
from
dotty-staging:port-gestalt-type-toolbox
Nov 12, 2018
Merged
Port gestalt TypeToolbox #5388
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
119 changes: 119 additions & 0 deletions
119
tests/run-separate-compilation/gestalt-type-toolbox-reflect/Macro_1.scala
This file contains hidden or 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,119 @@ | ||
// Port of https://github.com/liufengyun/gestalt/blob/master/macros/src/main/scala/gestalt/macros/TypeToolbox.scala | ||
// using staging reflection | ||
|
||
import scala.quoted._ | ||
import scala.tasty._ | ||
|
||
object TypeToolbox { | ||
/** are the two types equal? */ | ||
inline def =:=[A, B]: Boolean = ~tpEqImpl('[A], '[B]) | ||
private def tpEqImpl[A, B](a: Type[A], b: Type[B])(implicit reflect: Reflection): Expr[Boolean] = { | ||
import reflect._ | ||
val res = a.reflect.tpe =:= b.reflect.tpe | ||
res.toExpr | ||
} | ||
|
||
/** is `tp1` a subtype of `tp2` */ | ||
inline def <:<[A, B]: Boolean = ~tpLEqImpl('[A], '[B]) | ||
private def tpLEqImpl[A, B](a: Type[A], b: Type[B])(implicit reflect: Reflection): Expr[Boolean] = { | ||
import reflect._ | ||
val res = a.reflect.tpe <:< b.reflect.tpe | ||
res.toExpr | ||
} | ||
|
||
/** type associated with the tree */ | ||
inline def typeOf[T, Expected](a: T): Boolean = ~typeOfImpl('(a), '[Expected]) | ||
private def typeOfImpl(a: Expr[_], expected: Type[_])(implicit reflect: Reflection): Expr[Boolean] = { | ||
import reflect._ | ||
val res = a.reflect.tpe =:= expected.reflect.tpe | ||
res.toExpr | ||
} | ||
|
||
/** does the type refer to a case class? */ | ||
inline def isCaseClass[A]: Boolean = ~isCaseClassImpl('[A]) | ||
private def isCaseClassImpl(tp: Type[_])(implicit reflect: Reflection): Expr[Boolean] = { | ||
import reflect._ | ||
val res = tp.reflect.symbol match { | ||
case IsClassSymbol(sym) => sym.flags.isCase | ||
case _ => false | ||
} | ||
res.toExpr | ||
} | ||
|
||
/** val fields of a case class Type -- only the ones declared in primary constructor */ | ||
inline def caseFields[T]: List[String] = ~caseFieldsImpl('[T]) | ||
private def caseFieldsImpl(tp: Type[_])(implicit reflect: Reflection): Expr[List[String]] = { | ||
import reflect._ | ||
val fields = tp.reflect.symbol.asClass.caseFields.map(_.name) | ||
fields.toExpr | ||
} | ||
|
||
inline def fieldIn[T](inline mem: String): String = ~fieldInImpl('[T], mem) | ||
private def fieldInImpl(t: Type[_], mem: String)(implicit reflect: Reflection): Expr[String] = { | ||
import reflect._ | ||
val field = t.reflect.symbol.asClass.field(mem) | ||
field.map(_.name).getOrElse("").toExpr | ||
} | ||
|
||
inline def fieldsIn[T]: Seq[String] = ~fieldsInImpl('[T]) | ||
private def fieldsInImpl(t: Type[_])(implicit reflect: Reflection): Expr[Seq[String]] = { | ||
import reflect._ | ||
val fields = t.reflect.symbol.asClass.fields | ||
fields.map(_.name).toList.toExpr | ||
} | ||
|
||
inline def methodIn[T](inline mem: String): Seq[String] = ~methodInImpl('[T], mem) | ||
private def methodInImpl(t: Type[_], mem: String)(implicit reflect: Reflection): Expr[Seq[String]] = { | ||
import reflect._ | ||
t.reflect.symbol.asClass.classMethod(mem).map(_.name).toExpr | ||
} | ||
|
||
inline def methodsIn[T]: Seq[String] = ~methodsInImpl('[T]) | ||
private def methodsInImpl(t: Type[_])(implicit reflect: Reflection): Expr[Seq[String]] = { | ||
import reflect._ | ||
t.reflect.symbol.asClass.classMethods.map(_.name).toExpr | ||
} | ||
|
||
inline def method[T](inline mem: String): Seq[String] = ~methodImpl('[T], mem) | ||
private def methodImpl(t: Type[_], mem: String)(implicit reflect: Reflection): Expr[Seq[String]] = { | ||
import reflect._ | ||
t.reflect.symbol.asClass.method(mem).map(_.name).toExpr | ||
} | ||
|
||
inline def methods[T]: Seq[String] = ~methodsImpl('[T]) | ||
private def methodsImpl(t: Type[_])(implicit reflect: Reflection): Expr[Seq[String]] = { | ||
import reflect._ | ||
t.reflect.symbol.asClass.methods.map(_.name).toExpr | ||
} | ||
|
||
inline def typeTag[T](x: T): String = ~typeTagImpl('[T]) | ||
private def typeTagImpl(tp: Type[_])(implicit reflect: Reflection): Expr[String] = { | ||
import reflect._ | ||
val res = tp.reflect.tpe.showCode | ||
res.toExpr | ||
} | ||
|
||
inline def companion[T1, T2]: Boolean = ~companionImpl('[T1], '[T2]) | ||
private def companionImpl(t1: Type[_], t2: Type[_])(implicit reflect: Reflection): Expr[Boolean] = { | ||
import reflect._ | ||
val res = t1.reflect.symbol.asClass.companionClass.contains(t2.reflect.symbol) | ||
res.toExpr | ||
} | ||
|
||
inline def companionName[T1]: String = ~companionNameImpl('[T1]) | ||
private def companionNameImpl(tp: Type[_])(implicit reflect: Reflection): Expr[String] = { | ||
import reflect._ | ||
tp.reflect.symbol match { | ||
case IsClassSymbol(sym) => sym.companionClass.map(_.fullName).getOrElse("").toExpr | ||
case _ => '("") | ||
} | ||
} | ||
|
||
// TODO add to the std lib | ||
private implicit def listIsLiftable[T: Type: Liftable]: Liftable[List[T]] = new Liftable { | ||
def toExpr(list: List[T]): Expr[List[T]] = list match { | ||
case x :: xs => '(~x.toExpr :: ~toExpr(xs)) | ||
case Nil => '(Nil) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.