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
@scalaxy.extension[Any] | |
def quoted(quote: String): String = macro { | |
// `c` is defined as the macro Context. | |
// `quote` and `self` are in scope, defined as `c.Expr[Any]` and `c.Expr[String]`. | |
println(s"Currently expanding ${c.prefix}.quoted(${quote.tree})") | |
// `c.universe._` is automatically imported, so `reify` is in scope. | |
reify({ | |
// Make sure we're not evaluating quote twice, in case it's a complex expression! | |
val q = quote.splice | |
q + self.splice + q |
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
@scalaxy.extension[Any] | |
def quoted(quote: String) = quote + self + quote |
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
@extend(Any) def quoted(quote: String) = quote + self + quote |
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
implicit def Extensions(self: Any) = new Extensions(self) | |
class Extensions(self: Any) { | |
def quoted(quote: String) = quote + self + quote | |
} | |
println(10.quoted("'")) // prints "'10'" |
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
implicit class Extensions(self: Any) { | |
def quoted(quote: String) = quote + self + quote | |
} | |
println(10.quoted("'")) // prints "'10'" |
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
// Absolutely need to annotate return type! | |
@extend(URL) def secure: URL = macro | |
reify(new URL(s"https://${self.splice.getHost}${self.splice.getPath}")) | |
// Gets desugared to: | |
import scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
implicit class secure(self: URL) { | |
def secure = macro secureImpl.secureImpl |
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
replace( | |
{ | |
// $vparamss($x) are special variables that capture all parameter groups. | |
// $vparams($x) would only capture one group | |
@extend($type) def $someMethod($vparamss: $) = $body | |
}, | |
{ | |
implicit class $someMethod(self: $type) extends AnyRef { | |
def $someMethod($vparamss: $) = $body | |
// $fresh$x is a special variable that picks a fresh name with fresh("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
// Author: Olivier Chafik (http://ochafik.com) | |
// Feel free to modify and reuse this for any purpose ("public domain / I don't care"). | |
package scalaxy.pretyping.example | |
import scala.reflect.internal._ | |
import scala.tools.nsc.CompilerCommand | |
import scala.tools.nsc.Global | |
import scala.tools.nsc.Phase | |
import scala.tools.nsc.plugins.Plugin | |
import scala.tools.nsc.plugins.PluginComponent |
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
assert x.getSize() == 2 : "Size of x is not 2"; // Java assert |
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
// Only works with 2.10.0+ | |
scalaVersion := "2.10.0" | |
// Dependency at compilation-time only (not at runtime). | |
libraryDependencies += "com.nativelibs4java" %% "scalaxy-debug" % "0.3-SNAPSHOT" % "provided" | |
// Scalaxy/Debug snapshots are published on the Sonatype repository. | |
resolvers += Resolver.sonatypeRepo("snapshots") |