Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10160 from som-snytt/issue/12645-whitebox-inference
- Loading branch information
Showing
6 changed files
with
74 additions
and
14 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
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,20 @@ | ||
|
||
// scalac: -Xsource:3 | ||
|
||
import language.experimental.macros | ||
import scala.reflect.macros.whitebox.Context | ||
|
||
trait Greeter { | ||
def greeting: Option[Any] = Some("Take me to your leader, Greeter.") | ||
} | ||
|
||
class Welcomer { | ||
def greeter: Greeter = macro Macros.impl | ||
} | ||
|
||
object Macros { | ||
def impl(c: Context) = { | ||
import c.universe._ | ||
q"""new Greeter { override def greeting = Some("hello, quoted world") }""" | ||
} | ||
} |
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,8 @@ | ||
|
||
// scalac: -Xsource:3 | ||
|
||
object Test extends App { | ||
def f(s: String) = println(s) | ||
val welcomer = new Welcomer | ||
welcomer.greeter.greeting.foreach(f) | ||
} |
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,6 @@ | ||
// scalac: -Xsource:3 | ||
|
||
object Test extends App { | ||
Foo.ctx.quote(42).ast.id | ||
} | ||
// was: Test_2.scala:4: error: value id is not a member of Ast |
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,31 @@ | ||
|
||
// scalac: -Xsource:3 | ||
|
||
import scala.language.experimental.macros | ||
import scala.reflect.macros.whitebox.Context | ||
|
||
trait Ast | ||
case class Foo(id: Int) extends Ast | ||
object Foo { | ||
val ctx = new Ctx {} | ||
} | ||
|
||
trait Ctx { | ||
trait Quoted[+A] { | ||
def ast: Ast | ||
} | ||
final def quote[A](a: A): Quoted[A] = macro QuoteMacro.quoteImpl[A] | ||
} | ||
|
||
class QuoteMacro(val c: Context) { | ||
import c.universe.* | ||
|
||
def quoteImpl[A](a: c.Expr[A])(implicit t: WeakTypeTag[A]) = | ||
c.untypecheck { | ||
q""" | ||
new ${c.prefix}.Quoted[$t] { | ||
def ast = Foo(42) | ||
} | ||
""" | ||
} | ||
} |