Skip to content

Instantly share code, notes, and snippets.

@xeno-by
xeno-by / gist:5967900
Created Jul 10, 2013
Macro-powered structural types
View gist:5967900
import scala.annotation.StaticAnnotation
import scala.reflect.macros.Macro
import language.experimental.macros
class body(tree: Any) extends StaticAnnotation
trait Macros extends Macro {
import c.universe._
def selFieldImpl = {
@xeno-by
xeno-by / gist:8b64ca6a73775147941e
Last active Dec 4, 2018
Towards perfect compile-time proxies in Scala
View gist:8b64ca6a73775147941e
// PROBLEM STATEMENT
// Let's build Proxy[T], an object that can capture calls to methods of an underlying object.
// We want the proxy to be type-safe, i.e. we need to verify that the names of the calls
// and the arguments that are passed to those calls are well-typed wrt the type of the proxee.
object Test extends App {
trait Foo { /* ... */ }
val proxy: Proxy[Foo] = ???
proxy.bar()
View keybase.md

Keybase proof

I hereby claim:

  • I am xeno-by on github.
  • I am xeno_by (https://keybase.io/xeno_by) on keybase.
  • I have a public key whose fingerprint is FE5A 8474 50B9 0939 A0A5 6C10 E512 F2A2 36EE 59D9

To claim this, I am signing this object:

View keybase.md

Keybase proof

I hereby claim:

  • I am xeno-by on github.
  • I am xeno_by (https://keybase.io/xeno_by) on keybase.
  • I have a public key whose fingerprint is FA14 7F45 5509 FC4D 106C 13FA AB30 CC8E F90D C4A8

To claim this, I am signing this object:

@xeno-by
xeno-by / gist:2559714
Created Apr 30, 2012
Mixing in a trait dynamically
View gist:2559714
Answers http://stackoverflow.com/questions/10373318/mixing-in-a-trait-dynamically.
Compile as follows:
scalac Common_1.scala Macros_2.scala
scalac Common_1.scala Test_3.scala -cp <path to the result of the previous compilation>
Tested in 2.10.0-M3, will most likely not compile by the time 2.10.0 final is released, because we're actively rehashing the API.
However the principles will remain the same in the final release, so the concept itself is okay.
upd. Code updated for 2.10.0-M7.
upd. Code updated for 2.10.0-RC1.
View gist:41e342519d69d915cba799f9ba3eca9a
> publish
Generating Scalatex Sources...
[info] Resolving readme:readme_2.11:0.1-SNAPSHOT
[info] Resolution done
[info] Fetching artifacts
[info] Fetching artifacts: done
[info] Resolving org.scalameta:tokenizers_2.11:1.0.0-SNAPSHOT
[info] Resolution done
[info] Fetching artifacts
[info] Fetching artifacts: done
View gist:f888e6e92c3fbbea64c5efe8237f3d66
scala> 16:32 ~$ parse "x + (y)(z)"
[[syntax trees at end of parser]]// Scala source: tmpqW0GnW
package <empty> {
class wrapper extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
x.$plus(y(z))
}
View gist:8495463
BUILD FAILED
/localhome/jenkins/b/workspace/scala-checkin/build.xml:96: The following error occurred while executing this line:
/localhome/jenkins/b/workspace/scala-checkin/build-ant-macros.xml:8: The following error occurred while executing this line:
/localhome/jenkins/b/workspace/scala-checkin/build.xml:1420: The following error occurred while executing this line:
/localhome/jenkins/b/workspace/scala-checkin/build-ant-macros.xml:488: Test suite finished with 1 case failing:
fail - run/t6200.scala [output differs]% scalac t6200.scala
% /home/jenkins/apps/jdk1.6.0_33-x64/jre/bin/java \
-classpath \
/localhome/jenkins/b/workspace/scala-checkin/test/files/run/t6200-run.obj:/localhome/jenkins/b/workspace/scala-checkin/build/pack/lib/scala-library.jar:/localhome/jenkins/b/workspace/scala-checkin/build/pack/lib/scala-reflect.jar:/localhome/jenkins/b/workspace/scala-checkin/build/pack/lib/scala-compiler.jar:/localhome/jenkins/b/workspace/scala-checkin/build/pack/lib/scalap.jar:/localhome/jenkins/b/workspace/s
View gist:8439164
19:15 ~/Projects/Master/sandbox (master)$ scala
Welcome to Scala version 2.11.0-20140115-080235-07e823ad97 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_65).
Type in expressions to have them evaluated.
Type :help for more information.
scala> q"class C(x: Int)"
res0: reflect.runtime.universe.ClassDef =
class C extends scala.AnyRef {
<paramaccessor> private[this] val x: Int = _;
def <init>(x: Int) = {
View gist:8255893
import scala.reflect.macros.WhiteboxContext
import scala.language.experimental.macros
object Macros {
def impl(c: WhiteboxContext) = {
import c.universe._
val result = c.typecheck(q"""
class C[T <: Int] {
type Dummy123 = List[T]
}
You can’t perform that action at this time.