scala-macros

Is there a way to test at compile-time that a constant is a compile-time constant?

北战南征 提交于 2019-11-28 03:13:37
问题 Given how difficult it is to know whether an arithmetic final val expression will be compiled to a compile-time constant, and how easy it is to accidentally break compile-time-ness... Can anyone think of an easy way to verify, at compile-time, that the compiler has actually created a compile-time constant from, say, a complex arithmetic expression? I'm guessing this might be some kind of annotation or macro, but maybe there's something simpler. For example, maybe something like: @CompileTime

Where can I learn about constructing AST's for Scala macros?

北城以北 提交于 2019-11-28 03:02:21
Where I can learn how to construct the AST's that Scala's macros generate? The Scaladoc isn't as helpful as I'd like. For example: abstract def Apply(sym: Universe.Symbol, args: Universe.Tree*): Universe.Tree A factory method for Apply nodes. But how do I figure out what an Apply node is? Where can I find a list of the node types in AST's, and how they fit together? kiritsuku There isn't a lot of documentation for the internals of the compiler available, but the things that are available should be enough to get started. Mirko Stocker , has written his Master Thesis about Scala Refactoring . In

How can I create an instance of a Case Class with constructor arguments with no Parameters in Scala?

被刻印的时光 ゝ 提交于 2019-11-28 01:46:25
问题 I'm making a Scala app that sets by reflection field values. This works OK. However, in order to set field values I need a created instance. If I have a class with an empty constructor, I can do this easily with classOf[Person].getConstructors.... However, when I try doing this with a Case class with a non empty constructor It doesn't work. I have all of the field names and its values, as well as the Object type I need to create. Can I instance the Case Class somehow with what I've got? The

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

走远了吗. 提交于 2019-11-28 01:32:53
问题 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 Parameters on Scala Macro Annotations

馋奶兔 提交于 2019-11-28 01:30:26
问题 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

Custom Scala enum, most elegant version searched

馋奶兔 提交于 2019-11-27 22:13:53
问题 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

Scala macros: What is the difference between typed (aka typechecked) an untyped Trees

只愿长相守 提交于 2019-11-27 19:08:57
I'm getting started with scala macros, they're awesome, but I'm running into the difference between typed (aka typechecked) and untyped Tree s. For example, you can't call c.eval with a typechecked Tree for some reason. I can't find documentation on this 'typechecked' in the scala macros documentation (I know they're still working on that, this might be something that needs to be added some day). What does it mean for a Tree to be typechecked? Why are they so different that apparently c.eval can't deal with typechecked Tree s (the inverse would make more sense to me). I guess this is probably

Enabling the macro-paradise Scala compiler plugin in Maven projects

大城市里の小女人 提交于 2019-11-27 15:07:43
问题 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. 回答1: 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> ... <

Getting subclasses of a sealed trait

心已入冬 提交于 2019-11-27 14:09:24
Is it possible (via macros, some form of Shapeless automagic or otherwise) to obtain a list of the subclasses of a sealed trait: At compile time? At runtime? You don't need any 3rd party library to do this: sealed trait MyTrait case object SubClass1 extends MyTrait case object SubClass2 extends MyTrait import scala.reflect.runtime.{universe => ru} val tpe = ru.typeOf[MyTrait] val clazz = tpe.typeSymbol.asClass // if you want to ensure the type is a sealed trait, // then you can use clazz.isSealed and clazz.isTrait clazz.knownDirectSubclasses.foreach(println) Output: object SubClass1 object

Howto model named parameters in method invocations with Scala macros?

丶灬走出姿态 提交于 2019-11-27 13:10:58
There are use cases where it is useful to create a copy of an object which is an instance of a case class of a set of case classes, which have a specific value in common. For example let's consider the following case classes: case class Foo(id: Option[Int]) case class Bar(arg0: String, id: Option[Int]) case class Baz(arg0: Int, id: Option[Int], arg2: String) Then copy can be called on each of these case class instances: val newId = Some(1) Foo(None).copy(id = newId) Bar("bar", None).copy(id = newId) Baz(42, None, "baz").copy(id = newId) As described here and here there is no simple way to