-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support inline unapplySeq and with leading given parameters #16358
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 all commits
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 was deleted.
Oops, something went wrong.
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,11 @@ | ||
package i8577 | ||
|
||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
def implUnapply(sc: Expr[Macro.StrCtx], input: Expr[Int])(using Quotes): Expr[Option[Seq[Int]]] = | ||
'{ Some(Seq(${input})) } |
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,9 @@ | ||
package i8577 | ||
|
||
def main: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension (inline ctx: Macro.StrCtx) inline def unapplySeq(inline input: Int): Option[Seq[Int]] = | ||
${ implUnapply('ctx, 'input) } | ||
|
||
val mac"$x" = 1 | ||
assert(x == 1) |
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,11 @@ | ||
package i8577 | ||
|
||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
def implUnapply[U](sc: Expr[Macro.StrCtx], input: Expr[U])(using Type[U])(using Quotes): Expr[Option[Seq[U]]] = | ||
'{ Some(Seq(${input})) } |
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,9 @@ | ||
package i8577 | ||
|
||
def main: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: U): Option[Seq[U]] = | ||
${ implUnapply('ctx, 'input) } | ||
|
||
val mac"$x" = 1 | ||
assert(x == 1) |
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,11 @@ | ||
package i8577 | ||
|
||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
def implUnapply[T](sc: Expr[Macro.StrCtx], input: Expr[T])(using Type[T])(using Quotes): Expr[Option[Seq[T]]] = | ||
'{ Some(Seq(${input})) } |
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,9 @@ | ||
package i8577 | ||
|
||
def main: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq(inline input: T): Option[Seq[T]] = | ||
${ implUnapply('ctx, 'input) } | ||
|
||
val mac"$x" = 1 | ||
assert(x == 1) |
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,11 @@ | ||
package i8577 | ||
|
||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
def implUnapply[T](sc: Expr[Macro.StrCtx], input: Expr[T])(using Type[T])(using Quotes): Expr[Option[Seq[T]]] = | ||
'{ Some(Seq(${input})) } |
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,9 @@ | ||
package i8577 | ||
|
||
def main: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: T): Option[Seq[T]] = | ||
${ implUnapply('ctx, 'input) } | ||
|
||
val mac"$x" = 1 | ||
assert(x == 1) |
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,11 @@ | ||
package i8577 | ||
|
||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
def implUnapply[T, U](sc: Expr[Macro.StrCtx], input: Expr[U])(using Type[U])(using Quotes): Expr[Option[Seq[U]]] = | ||
'{ Some(Seq(${input})) } |
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,9 @@ | ||
package i8577 | ||
|
||
def main: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: U): Option[Seq[U]] = | ||
${ implUnapply('ctx, 'input) } | ||
|
||
val mac"$x" = 1 | ||
assert(x == 1) |
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,11 @@ | ||
package i8577 | ||
|
||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
def implUnapply[T, U](sc: Expr[Macro.StrCtx], input: Expr[(T, U)])(using Type[T], Type[U])(using Quotes): Expr[Option[Seq[(T, U)]]] = | ||
'{ Some(Seq(${input})) } |
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,12 @@ | ||
package i8577 | ||
|
||
def main: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: (T, U)): Option[Seq[(T, U)]] = | ||
${ implUnapply('ctx, 'input) } | ||
|
||
val mac"$x" = (1, 2) | ||
assert(x == (1, 2)) | ||
|
||
val mac"$y" = (1, "a") | ||
assert(y == (1, "a")) |
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,11 @@ | ||
package i8577 | ||
|
||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
def implUnapply[T, U](sc: Expr[Macro.StrCtx], input: Expr[T | U])(using Type[T], Type[U])(using Quotes): Expr[Option[Seq[T | U]]] = | ||
'{ Some(Seq(${input})) } |
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,9 @@ | ||
package i8577 | ||
|
||
def main: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: T | U): Option[Seq[T | U]] = | ||
${ implUnapply('ctx, 'input) } | ||
|
||
val mac"$x" = 1 | ||
assert(x == 1) |
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,11 @@ | ||
package i8577 | ||
|
||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
def implUnapply[T, U](sc: Expr[Macro.StrCtx], input: Expr[T | U])(using Type[T], Type[U])(using Quotes): Expr[Option[Seq[T | U]]] = | ||
'{ Some(Seq(${input})) } |
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,9 @@ | ||
package i8577 | ||
|
||
def main: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: U | T): Option[Seq[T | U]] = | ||
${ implUnapply('ctx, 'input) } | ||
|
||
val mac"$x" = 1 | ||
assert(x == 1) |
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,15 @@ | ||
object Foo: | ||
inline def unapply(using String)(i: Int): Some[Int] = Some(i) | ||
|
||
object Bar: | ||
inline def unapply(using String)(using String)(i: Int): Some[Int] = Some(i) | ||
|
||
object Baz: | ||
inline def unapply[T](using String)(i: T): Some[T] = Some(i) | ||
|
||
given String = "" | ||
|
||
val i = 10 match | ||
case Foo(x) => x | ||
case Bar(x) => x | ||
case Baz(x) => x |
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,9 @@ | ||
object O | ||
|
||
extension [T] (ctx: O.type) inline def unapplySeq(input: T): Option[Seq[T]] = Some(Seq(input)) | ||
|
||
@main | ||
def Main = { | ||
val O(x) = 3 | ||
println(s"x: $x") | ||
} |
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,8 @@ | ||
class C | ||
|
||
extension (ctx: C) inline def unapply(input: String): Option[String] = Some("hi") | ||
|
||
@main def run = { | ||
val O = new C | ||
val O(x) = "3" | ||
} |
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,21 @@ | ||
package i8577 | ||
|
||
type A; given A: A = ???; | ||
type B; given B: B = ???; | ||
type C; given C: C = ???; | ||
type D; given D: D = ???; | ||
type E; given E: E = ???; | ||
type F; given F: F = ???; | ||
|
||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
def main: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (using A)(inline ctx: Macro.StrCtx)(using B) inline def unapplySeq[U](using C)(inline input: T)(using D)(using F): Option[Seq[T]] = ??? | ||
|
||
(??? : Int) match | ||
case mac"${x}" => 1 |
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,15 @@ | ||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension (inline ctx: Macro.StrCtx) inline def unapplySeq(inline input: Int): Option[Seq[Int]] = | ||
Some(Seq(input)) | ||
|
||
@main def Test: Unit = | ||
val mac"$x" = 1 | ||
val y: Int = x | ||
assert(x == 1) |
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,15 @@ | ||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: U): Option[Seq[U]] = | ||
Some(Seq(input)) | ||
|
||
@main def Test: Unit = | ||
val mac"$x" = 1 | ||
val y: Int = x | ||
assert(x == 1) |
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,15 @@ | ||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq(inline input: T): Option[Seq[T]] = | ||
Some(Seq(input)) | ||
|
||
@main def Test: Unit = | ||
val mac"$x" = 1 | ||
val y: Int = x | ||
assert(x == 1) |
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,15 @@ | ||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: T): Option[Seq[T]] = | ||
Some(Seq(input)) | ||
|
||
@main def Test: Unit = | ||
val mac"$x" = 1 | ||
val y: Int = x | ||
assert(x == 1) |
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,19 @@ | ||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension [T] (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: (T, U)): Option[Seq[(T, U)]] = | ||
Some(Seq(input)) | ||
|
||
@main def Test: Unit = | ||
val mac"$x" = (1, 2) | ||
val x2: (Int, Int) = x | ||
assert(x == (1, 2)) | ||
|
||
val mac"$y" = (1, "a") | ||
val y2: (Int, String) = y | ||
assert(y == (1, "a")) |
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,15 @@ | ||
import scala.quoted._ | ||
|
||
object Macro: | ||
opaque type StrCtx = StringContext | ||
def apply(ctx: StringContext): StrCtx = ctx | ||
def unapply(ctx: StrCtx): Option[StringContext] = Some(ctx) | ||
|
||
@main def Test: Unit = | ||
extension (ctx: StringContext) def mac: Macro.StrCtx = Macro(ctx) | ||
extension (inline ctx: Macro.StrCtx) inline def unapplySeq[U](inline input: U): Option[Seq[U]] = | ||
Some(Seq(input)) | ||
|
||
val mac"$x" = 1 | ||
val y: Int = x | ||
assert(x == 1) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this needed anymore? The comment above could maybe explain how type parameters are handled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's handled by
fun.tpe.widen
(line 202)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment below and managed to simplify the leading implicits logic thanks to the use of
fun.tpe.widen
.Now it even works with something as complex as