scala-macros

Type Parameters on Scala Macro Annotations

喜你入骨 提交于 2019-11-29 08:02:13
I'm trying to use macro annotations in scala, where my macro annotation would take an argument of another type. It would then use scala reflection to look at the passed in type, and add some methods as appropriate.Eg. trait MyTrait { def x: Int def y: Float } @MyAnnotation class MyClass //<-- somehow, this annotation should reference MyTrait class MyAnnotation(val target: Any) extends StaticAnnotation { def macroTransform(annottees: Any*) = macro MyAnnotationImpl.impl } object MyAnnotationImpl { def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { // if I can get a handle on the type

Scala Macro Annotations: c.TypeCheck of annotated type causes StackOverflowError

时光总嘲笑我的痴心妄想 提交于 2019-11-29 07:54:41
I have a macro annotation which is intended to be applied to class definitions. Its purpose is an almost-but-not-quite serialization tool. It inspects the class's constructor parameters and then creates a factory method on the companion object which in turn supplies values for the parameters. It needs to know the types of the parameters in order to do this, so I've been calling Context.typeCheck on them. The problem occurs when the annotated class's constructor takes a parameter of the same type as itself, or in other similar situations (for instance, if type A and type B both are annotated,

Scala macro to print code?

大兔子大兔子 提交于 2019-11-29 07:32:31
I want to do something like this: def assuming[A](condition: => Boolean)(f: => A): A = { require(condition, /* print source-code of condition */) f } Sample usage: def fib(n: Int) = n match { // yes, yes, I know this is not efficient case 0 => 0 case 1 => 1 case i => assuming(i > 0) { fib(i-1) + fib(i-2) } } Now, for example, if you call fib(-20) , I want it to throw an exception with a message like Assertion failed: -20 > 0 or Assertation failed: i > 0 Dude, isn't an assert macro one of the basic use cases you implement to learn how to use macros? Well, that's what I thought, too. By "glean

Custom Scala enum, most elegant version searched

风格不统一 提交于 2019-11-29 03:52:57
For a project of mine I have implemented a Enum based upon trait Enum[A] { trait Value { self: A => _values :+= this } private var _values = List.empty[A] def values = _values } sealed trait Currency extends Currency.Value object Currency extends Enum[Currency] { case object EUR extends Currency case object GBP extends Currency } from Case objects vs Enumerations in Scala . I worked quite nice, till I run into the following problem. Case objects seem to be lazy and if I use Currency.value I might actually get an empty List. It would have been possible to make a call against all Enum Values on

How to make IntelliJ IDEA recognise code created by macros?

不打扰是莪最后的温柔 提交于 2019-11-29 02:09:15
问题 Background I have an sbt-managed Scala project that uses the usual sbt project layout for Scala projects with macros, i.e., a subproject that contains the macros a main project that is the actual application and that depends on the macro subproject. The macros are macro annotations which, in essence, generate companion objects for regular classes. The generated companion objects declare, amongst other members, apply/unapply methods. I used the sbt-idea plugin to generate a corresponding

Enabling the macro-paradise Scala compiler plugin in Maven projects

两盒软妹~` 提交于 2019-11-28 23:32:39
I have the ordinary scala-2.10 macros working in a maven project just by including the scala-reflect.jar library as a dependency in the pom, but what do I need to turn on macro-paradise? I am using scala-2.10 and scala-maven-plugin-3.1.5. Looks like I got it to work with the following additions to the pom.xml <repositories> ... <repository> <id>oss.sonatype.org</id> <name>sonatype sapshots</name> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </repository> ... </repositories> and <plugins> ... <plugin> ... scala-maven-plugin identification... <configuration> ...

Documenting Scala 2.10 macros [closed]

孤人 提交于 2019-11-28 15:55:05
问题 I'll start with an example. Here's an equivalent of List.fill for tuples as a macro in Scala 2.10: import scala.language.experimental.macros import scala.reflect.macros.Context object TupleExample { def fill[A](arity: Int)(a: A): Product = macro fill_impl[A] def fill_impl[A](c: Context)(arity: c.Expr[Int])(a: c.Expr[A]) = { import c.universe._ arity.tree match { case Literal(Constant(n: Int)) if n < 23 => c.Expr( Apply( Select(Ident("Tuple" + n.toString), "apply"), List.fill(n)(a.tree) ) )

Getting Parameters from Scala Macro Annotation

纵饮孤独 提交于 2019-11-28 11:13:32
So I have an annotation on a function (DefDef). This annotation has parameters. However, I am confused on how to get the parameters from the constructor. Usage example: class TestMacro { @Foo(true) def foo(): String = "" foo } Here's the code for the annotation: class Foo(b: Boolean) extends StaticAnnotation { def macroTransform(annottees: Any*) = macro Foo.impl } object Foo { def impl(c: whitebox.Context)(annottees: c.Tree*): c.Expr[Any] = { import c.universe._ //how do I get value of `b` here??? c.abort(c.enclosingPosition, "message") } } What about this: val b: Boolean = c.prefix.tree match

How to use scala macros to create a function object (to create a Map[String, (T) => T])

ε祈祈猫儿з 提交于 2019-11-28 05:21:57
问题 I am trying to use Scala macros to create a case class map of single-parameter copy methods, with each method accepting a Play Json JsValue and a case class instance, and returning an updated copy of the instance. However, I am running into problems with the macro syntax for returning a function object. Given a case class case class Clazz(id: Int, str: String, strOpt: Option[String]) the intention is to create a map of the class's copy methods implicit def jsonToInt(json: JsValue) = json.as

Getting a structural type with an anonymous class's methods from a macro

馋奶兔 提交于 2019-11-28 03:30:27
Suppose we want to write a macro that defines an anonymous class with some type members or methods, and then creates an instance of that class that's statically typed as a structural type with those methods, etc. This is possible with the macro system in 2.10.0, and the type member part is extremely easy: object MacroExample extends ReflectionUtils { import scala.language.experimental.macros import scala.reflect.macros.Context def foo(name: String): Any = macro foo_impl def foo_impl(c: Context)(name: c.Expr[String]) = { import c.universe._ val Literal(Constant(lit: String)) = name.tree val