scala-macros

Compilation issue when accessing parameter value in Scala macro

泄露秘密 提交于 2019-12-11 04:53:56
问题 The calling code for this minimal example appears to compile (Eclipse Indigo SR2, Scala v2.10.20), but the project containing the calling code is marked with a red cross (not clear to me how to get further diagnostics for this). No class files are generated. If I replace param.value with some literal e.g. 1, the calling code compiles. Is this a known problem? Is there a workaround? def myMacro( param : Int ): Int = macro myMacroImpl( param ) def myMacroImpl(c: Context)(param: c.Expr[Int]): c

Scala Macro: Define Top Level Object

*爱你&永不变心* 提交于 2019-12-11 03:32:52
问题 I looked type macros for scala. But when i would like create object from example, i got error: Example.scala:7: `=', `>:', or `<:' expected type Test(url: String) = macro impl Example.scala:12: illegal start of simple expression val clazz = ClassDef(..., Template(..., generateCode())) Code: //Example.sbt object Example { type Test(url: String) = macro impl def impl(c:Context)(url: c.Expr[String]):c.Tree = { import c.universe._ val name = c.freshName(c.enclosingImpl.name).toTypeName val clazz

Scala presentation compiler locateTree method

[亡魂溺海] 提交于 2019-12-11 01:24:38
问题 I've been using scala presentation compiler API, or to be more exact it's locateTree method to get AST of some piece of source code, and then get its raw representation via showRaw(ast) call, but the result seems to be different compared to what I expected. For instance val tree = q"final def x = 1" println(showRaw(tree)) Outputs DefDef(Modifiers(FINAL), TermName("x"), List(), List(), TypeTree(), Literal(Constant(1))) , while a call to presentation compiler on the same source produces DefDef

Scala HList manipulation : is a flexible structure possible?

大憨熊 提交于 2019-12-11 01:05:15
问题 I wrote a min example of something I do not manage to do. Am I in the wrong way? object minNotWoringkExample { import shapeless._ def typed[T](t: => T) {} class Bar { type L <: HList } class Foo { type UPDATE[L <: HList] <: HList } class FooI extends Foo { type UPDATE[L <: HList] = FilterNot[L, FooI]#Out } def update[O <: Foo, B <: Bar] = new Bar { type L = O#UPDATE[B#L] } val myBar = new Bar { type L = FooI :: Foo :: HNil } val myUpdatedBar = update[FooI, Bar {type L = FooI :: Foo :: HNil}]

Scala macro get enclosing line

試著忘記壹切 提交于 2019-12-10 17:59:53
问题 I have the following macro object Example { def doIt(s: String): String = macro doItImpl def doItImpl(c: Context)(s: c.Expr[String]): c.Expr[String] = { import c.{ universe => u } import u._ ??? } } Instead of the ??? I would like to inspect the line where the method doIt was called to check if the result of the method was used (in an assignment, method call or whatever). If the result is not used I respond with an error-message. Call-example: val s: String = doIt("Hallo") // alright doIt(

SBT Plugin: How to add compiler plugin as a dependency that is not propagated downstream?

若如初见. 提交于 2019-12-10 14:48:20
问题 I'm writing a SBT plugin. I would like to use Circe JSON library, but it requires the Macro Paradise compiler plugin on Scala 2.10. Normally you add compiler plugins to build.sbt and SBT plugins to project/plugins.sbt . Now when you're building a SBT plugin, the other plugins become dependencies, so you put them to build.sbt and they are propagated to the projects where you use your SBT plugin. When I put the following snippet in build.sbt of my SBT plugin: addCompilerPlugin("org.scalamacros"

scala macros: how to read an annotation object

本秂侑毒 提交于 2019-12-10 14:14:04
问题 I want to do a similar thing as Scala Macros: Checking for a certain annotation My annotation looks like: class extract(val name: String) extends StaticAnnotation And I'm using it like this: case class MainClass(@extract("strings") foo: String, bar: Int) I'm trying to get foo parameter Symbol because it has an @extract annotation: val extrList = params.map { param: Symbol => param.annotations.collect { case extr if extr.tpe <:< c.weakTypeOf[extract] => val args = extr.scalaArgs if (args.size

Macro dependancy appearing in POM/JAR

萝らか妹 提交于 2019-12-10 10:56:17
问题 I have a scala project that uses macros which basically follows the exact method described here (http://www.scala-sbt.org/0.12.4/docs/Detailed-Topics/Macro-Projects.html) including the whole Distribution section (so in essence I have a root project, and a subproject called macro which holds the macros being used) The problem is, when I publish my project (using publish-local for now), and another scala project uses the one with a macro as a dependency, it tries to pull macro#macro_2.10;0.1

How to get Scala function's parameters / return type?

早过忘川 提交于 2019-12-10 04:24:36
问题 I have a function, and would like to obtain its parameter types and return type for use in Scala macros. scala> val fn = (a: String, b: Double) => 123 fn: (String, Double) => Int = <function2> scala> fn.getClass res1: Class[_ <: (String, Double) => Int] = class $anonfun$1 In the above example, the parameter types and return type already get printed at both lines, but I don't know how to access them. Even with toString I'd be stuck with the <function2> and class $anonfun$1 parts right of the =

Checking for varargs type ascription in Scala macros

久未见 提交于 2019-12-10 03:26:38
问题 Suppose I have this macro: import language.experimental.macros import scala.reflect.macros.Context object FooExample { def foo[A](xs: A*): Int = macro foo_impl[A] def foo_impl[A](c: Context)(xs: c.Expr[A]*) = c.literal(xs.size) } This works as expected with "real" varargs: scala> FooExample.foo(1, 2, 3) res0: Int = 3 But the behavior with a sequence ascribed to the varargs type is confusing to me (in Scala 2.10.0-RC3): scala> FooExample.foo(List(1, 2, 3): _*) res1: Int = 1 And to show that