scala-macros

Use Scala macros to generate methods

送分小仙女□ 提交于 2019-12-03 13:00:20
问题 I want to generate aliases of methods using annotation macros in Scala 2.11+. I am not even sure that is even possible. If yes, how? Example - Given this below, I want the annotation macros to expand into class Socket { @alias(aliases = Seq("!", "ask", "read")) def load(n: Int): Seq[Byte] = {/* impl */} } I want the above to generate the synonym method stubs as follows: class Socket { def load(n: Int): Seq[Byte] = // .... def !(n: Int) = load(n) def ask(n: Int) = load(n) def read(n: Int) =

Scala Macros: Accessing members with quasiquotes

做~自己de王妃 提交于 2019-12-03 10:04:01
问题 I'm trying to implement an implicit materializer as described here: http://docs.scala-lang.org/overviews/macros/implicits.html I decided to create a macro that converts a case class from and to a String using quasiquotes for prototyping purposes. For example: case class User(id: String, name: String) val foo = User("testid", "foo") Converting foo to text should result in "testid foo" and vice versa. Here is the simple trait and its companion object I have created: trait TextConvertible[T] {

StringContext and macros: a simple example

时光怂恿深爱的人放手 提交于 2019-12-03 09:32:37
问题 I'm trying to achieve a StringContext extension which will allow me to write this: val tz = zone"Europe/London" //tz is of type java.util.TimeZone But with the added caveat that it should fail to compile if the supplied time-zone is invalid (assuming that can be determined at compile-time). Here's a helper function: def maybeTZ(s: String): Option[java.util.TimeZone] = java.util.TimeZone.getAvailableIDs collectFirst { case id if id == s => java.util.TimeZone.getTimeZone(id) } I can create a

How to debug a macro annotation?

假如想象 提交于 2019-12-03 08:34:28
Background I'm trying to add a @Prisms annotation to Monocle that will work as follows. Given: @Prisms sealed trait Foo case object A extends Foo case object B extends Foo it will generate a companion object for Foo : object Foo { val a = monocle.macros.GenPrism.apply[Foo, A] val b = monocle.macros.GenPrism.apply[Foo, B] } (or if the companion object already exists, it will add those methods to it). The macro relies on directKnownSubclasses to find A and B , so there will be a note in the Monocle readme recommending that people use Typelevel Scala. I've configured Monocle to use TLS on my

How to use Type calculated in Scala Macro in a reify clause?

坚强是说给别人听的谎言 提交于 2019-12-03 05:11:53
问题 I've been working with Scala Macros and have the following code in the macro: val fieldMemberType = fieldMember.typeSignatureIn(objectType) match { case NullaryMethodType(tpe) => tpe case _ => doesntCompile(s"$propertyName isn't a field, it must be another thing") } reify{ new TypeBuilder() { type fieldType = fieldMemberType.type } } As you can see, I've managed to get a c.universe.Type fieldMemberType . This represents the type of certain field in the object. Once I get that, I want to

Scala Macros: Accessing members with quasiquotes

孤者浪人 提交于 2019-12-03 00:32:16
I'm trying to implement an implicit materializer as described here: http://docs.scala-lang.org/overviews/macros/implicits.html I decided to create a macro that converts a case class from and to a String using quasiquotes for prototyping purposes. For example: case class User(id: String, name: String) val foo = User("testid", "foo") Converting foo to text should result in "testid foo" and vice versa. Here is the simple trait and its companion object I have created: trait TextConvertible[T] { def convertTo(obj: T): String def convertFrom(text: String): T } object TextConvertible { import

StringContext and macros: a simple example

自作多情 提交于 2019-12-02 23:52:17
I'm trying to achieve a StringContext extension which will allow me to write this: val tz = zone"Europe/London" //tz is of type java.util.TimeZone But with the added caveat that it should fail to compile if the supplied time-zone is invalid (assuming that can be determined at compile-time). Here's a helper function: def maybeTZ(s: String): Option[java.util.TimeZone] = java.util.TimeZone.getAvailableIDs collectFirst { case id if id == s => java.util.TimeZone.getTimeZone(id) } I can create a non-macro implementation very easily: scala> implicit class TZContext(val sc: StringContext) extends

annotation macro that rewrites and impls a trait, generics not processed correctly

旧时模样 提交于 2019-12-02 10:04:57
I am writing a macro that needs to create a class that rewrites a trait, having the same methods/args of the trait but different return type. So say we got: trait MyTrait[T] { def x(t1: T)(t2: T): T } @AnnProxy class MyClass[T] extends MyTrait[T] MyClass will be rewritten to: class MyClass[T] { def x(t1: T)(t2: T): R[T] } (so x will now return R[T] instead of T) I wrote the macro and debugging it, it produces this code: Expr[Any](class MyClass[T] extends scala.AnyRef { def <init>() = { super.<init>(); () }; def x(t1: T)(t2: T): macrotests.R[T] = $qmark$qmark$qmark }) @AnnProxy As you see the

scala macros: Add function to class

我们两清 提交于 2019-12-01 19:39:11
I'm new to scala macros and I'm using scala 2.10.0-RC3. I want to write a macro that adds a function to a class. Usage example: trait MyTrait { def addF = macro { /*add "def f = 3" to class*/ } } class MyClass extends MyTrait { addF //Adds the "def f" to MyClass } object Main { val t = new MyClass assert(t.f==3) } I need this in the following scenario. My first try didn't use macros but didn't work, because I can't inherit the same trait twice. trait AddF[T] { def f(t: T) { /* ...do sthg ... */ } } class MyClass extends AddF[Int] with AddF[String] With the macro solution I could write class

scala macros: Add function to class

南楼画角 提交于 2019-12-01 19:16:56
问题 I'm new to scala macros and I'm using scala 2.10.0-RC3. I want to write a macro that adds a function to a class. Usage example: trait MyTrait { def addF = macro { /*add "def f = 3" to class*/ } } class MyClass extends MyTrait { addF //Adds the "def f" to MyClass } object Main { val t = new MyClass assert(t.f==3) } I need this in the following scenario. My first try didn't use macros but didn't work, because I can't inherit the same trait twice. trait AddF[T] { def f(t: T) { /* ...do sthg ...