-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Introduce MethodTypeKind to quotes reflection API #20249
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
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,6 +211,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => | |
* +- MatchCase | ||
* +- TypeBounds | ||
* +- NoPrefix | ||
* | ||
* +- MethodTypeKind | ||
* | ||
* +- Selector -+- SimpleSelector | ||
* +- RenameSelector | ||
|
@@ -3234,6 +3236,22 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => | |
/** `TypeTest` that allows testing at runtime in a pattern match if a `TypeRepr` is a `MethodOrPoly` */ | ||
given MethodOrPolyTypeTest: TypeTest[TypeRepr, MethodOrPoly] | ||
|
||
/** Type which decides on the kind of parameter list represented by `MethodType`. */ | ||
type MethodTypeKind | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered making this a concrete, static There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, it could work. |
||
|
||
/** Module object of `type MethodKind` */ | ||
val MethodTypeKind: MethodTypeKindModule | ||
|
||
/** Methods of the module object `val MethodKind` */ | ||
trait MethodTypeKindModule { this: MethodTypeKind.type => | ||
/** Represents a parameter list without any implicitness of parameters, like (x1: X1, x2: X2, ...) */ | ||
val Plain: MethodTypeKind | ||
/** Represents a parameter list with implicit parameters, like `(implicit X1, ..., Xn)`, `(using X1, ..., Xn)`, `(using x1: X1, ..., xn: Xn)` */ | ||
val Implicit: MethodTypeKind | ||
/** Represents a parameter list of a contextual method, like `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */ | ||
val Contextual: MethodTypeKind | ||
} | ||
|
||
/** Type of the definition of a method taking a single list of parameters. It's return type may be a MethodType. */ | ||
type MethodType <: MethodOrPoly | ||
|
||
|
@@ -3246,6 +3264,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => | |
/** Methods of the module object `val MethodType` */ | ||
trait MethodTypeModule { this: MethodType.type => | ||
def apply(paramNames: List[String])(paramInfosExp: MethodType => List[TypeRepr], resultTypeExp: MethodType => TypeRepr): MethodType | ||
def apply(kind: MethodTypeKind)(paramNames: List[String])(paramInfosExp: MethodType => List[TypeRepr], resultTypeExp: MethodType => TypeRepr): MethodType | ||
def unapply(x: MethodType): (List[String], List[TypeRepr], TypeRepr) | ||
} | ||
|
||
|
@@ -3255,8 +3274,12 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching => | |
/** Extension methods of `MethodType` */ | ||
trait MethodTypeMethods: | ||
extension (self: MethodType) | ||
/** Is this the type of using parameter clause `(implicit X1, ..., Xn)`, `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */ | ||
/** Is this the type of parameter clause like `(implicit X1, ..., Xn)`, `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */ | ||
def isImplicit: Boolean | ||
/** Is this the type of parameter clause like `(using X1, ..., Xn)` or `(using x1: X1, x2: X2, ... )` */ | ||
def isContextual: Boolean | ||
/** Returns a MethodTypeKind object representing the implicitness of the MethodType parameter clause. */ | ||
def methodTypeKind: MethodTypeKind | ||
/** Is this the type of erased parameter clause `(erased x1: X1, ..., xn: Xn)` */ | ||
@deprecated("Use `hasErasedParams` and `erasedParams`", "3.4") | ||
def isErased: Boolean | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
trait Foo | ||
trait Bar | ||
|
||
object Methods: | ||
def implicitMethod(implicit foo: Foo, int: Int): Bar = ??? | ||
def contextualMethod(using foo: Foo, int: Int): Bar = ??? | ||
def plainMethod(foo: Foo, int: Int): Bar = ??? | ||
|
||
object Macro: | ||
import scala.quoted._ | ||
inline def macroCall(): Unit = ${ macroCallImpl } | ||
def macroCallImpl(using Quotes): Expr[Unit] = | ||
testReadingMethodTypeKind | ||
testCreatingMethodTypeKind | ||
'{()} | ||
|
||
def testReadingMethodTypeKind(using Quotes) = | ||
import quotes.reflect._ | ||
def getFromMethods(name: String): TypeRepr = | ||
val typeRepr = TypeRepr.of[Methods.type] | ||
val symbol = | ||
typeRepr.typeSymbol.methodMember(name).headOption.getOrElse( | ||
typeRepr.typeSymbol.fieldMember(name) | ||
) | ||
typeRepr.memberType(symbol) | ||
|
||
assert(getFromMethods("implicitMethod").asInstanceOf[MethodType].isImplicit) | ||
assert(!getFromMethods("implicitMethod").asInstanceOf[MethodType].isContextual) | ||
assert(getFromMethods("implicitMethod").asInstanceOf[MethodType].methodTypeKind == MethodTypeKind.Implicit) | ||
|
||
assert(getFromMethods("contextualMethod").asInstanceOf[MethodType].isImplicit) | ||
assert(getFromMethods("contextualMethod").asInstanceOf[MethodType].isContextual) | ||
assert(getFromMethods("contextualMethod").asInstanceOf[MethodType].methodTypeKind == MethodTypeKind.Contextual) | ||
|
||
assert(!getFromMethods("plainMethod").asInstanceOf[MethodType].isImplicit) | ||
assert(!getFromMethods("plainMethod").asInstanceOf[MethodType].isContextual) | ||
assert(getFromMethods("plainMethod").asInstanceOf[MethodType].methodTypeKind == MethodTypeKind.Plain) | ||
|
||
|
||
def testCreatingMethodTypeKind(using Quotes) = | ||
import quotes.reflect._ | ||
val paramTypes = List(TypeRepr.of[Foo], TypeRepr.of[Int]) | ||
val resType = TypeRepr.of[Bar] | ||
val implicitMethodType = MethodType.apply(MethodTypeKind.Implicit)(List("foo", "int"))(mt => paramTypes, mt => resType) | ||
assert(implicitMethodType.isImplicit) | ||
assert(!implicitMethodType.isContextual) | ||
assert(implicitMethodType.methodTypeKind == MethodTypeKind.Implicit) | ||
assert(implicitMethodType.methodTypeKind != MethodTypeKind.Contextual) | ||
assert(implicitMethodType.methodTypeKind != MethodTypeKind.Plain) | ||
|
||
|
||
val contextualMethodType = MethodType.apply(MethodTypeKind.Contextual)(List("foo", "int"))(mt => paramTypes, mt => resType) | ||
assert(contextualMethodType.isImplicit) | ||
assert(contextualMethodType.isContextual) | ||
assert(contextualMethodType.methodTypeKind != MethodTypeKind.Implicit) | ||
assert(contextualMethodType.methodTypeKind == MethodTypeKind.Contextual) | ||
assert(contextualMethodType.methodTypeKind != MethodTypeKind.Plain) | ||
|
||
val plainMethodType = MethodType.apply(MethodTypeKind.Plain)(List("foo", "int"))(mt => paramTypes, mt => resType) | ||
assert(!plainMethodType.isContextual) | ||
assert(!plainMethodType.isImplicit) | ||
assert(plainMethodType.methodTypeKind != MethodTypeKind.Implicit) | ||
assert(plainMethodType.methodTypeKind != MethodTypeKind.Contextual) | ||
assert(plainMethodType.methodTypeKind == MethodTypeKind.Plain) |
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,3 @@ | ||
object Test: | ||
def main(args: Array[String]): Unit = | ||
Macro.macroCall() |
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.