forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blackbox restriction #4: can't customize pattern matching
When an application of a blackbox macro is used as an extractor in a pattern match, it triggers an unconditional compiler error, preventing customizations of pattern matching implemented with macros.
- Loading branch information
Showing
8 changed files
with
62 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Test_2.scala:3: error: extractor macros can only be whitebox | ||
case Extractor(x) => println(x) | ||
^ | ||
one error found |
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,21 @@ | ||
import scala.reflect.macros.BlackboxContext | ||
import language.experimental.macros | ||
|
||
object Extractor { | ||
def unapply(x: Int) = macro Macros.unapplyImpl | ||
} | ||
|
||
object Macros { | ||
def unapplyImpl(c: BlackboxContext)(x: c.Tree) = { | ||
import c.universe._ | ||
q""" | ||
new { | ||
class Match(x: Int) { | ||
def isEmpty = false | ||
def get = x | ||
} | ||
def unapply(x: Int) = new Match(x) | ||
}.unapply($x) | ||
""" | ||
} | ||
} |
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,5 @@ | ||
object Test extends App { | ||
42 match { | ||
case Extractor(x) => println(x) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Test_2.scala:4: error: extractor macros can only expand into extractor calls | ||
Test_2.scala:4: error: extractor macros can only be whitebox | ||
case t"$x" => println(x) | ||
^ | ||
one error found |
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 @@ | ||
42 |
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,21 @@ | ||
import scala.reflect.macros.WhiteboxContext | ||
import language.experimental.macros | ||
|
||
object Extractor { | ||
def unapply(x: Int) = macro Macros.unapplyImpl | ||
} | ||
|
||
object Macros { | ||
def unapplyImpl(c: WhiteboxContext)(x: c.Tree) = { | ||
import c.universe._ | ||
q""" | ||
new { | ||
class Match(x: Int) { | ||
def isEmpty = false | ||
def get = x | ||
} | ||
def unapply(x: Int) = new Match(x) | ||
}.unapply($x) | ||
""" | ||
} | ||
} |
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,5 @@ | ||
object Test extends App { | ||
42 match { | ||
case Extractor(x) => println(x) | ||
} | ||
} |