scala-2.10

How to build a dynamic sequence in a scala macro?

烂漫一生 提交于 2019-12-21 17:53:13
问题 I have a scala macro which outputs nested case classes. I can assemble fragments of expressions created using reify to build up the nested case classes programmatically: case class Foo(name: String) case class Bar(foo: Foo) def foo(name: String) = { c.universe reify { Foo(c.literal(name).splice) } } def bar(foo: Expr[Foo]) = { c.universe reify { Bar(foo.splice) } } // output Bar(Foo("MyFoo")) c.Expr( bar(foo("MyFoo").asInstanceOf[Expr[Foo]]).tree ) Things work well apart from the annoying

Creating a method definition tree from a method symbol and a body

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-21 09:45:42
问题 Is there a convenient way to turn a MethodSymbol into the left-hand side of a method definition tree (i.e., a DefDef) in Scala 2.10? For example, suppose I want to create a macro that will take an instance of a trait and wrap all of that trait's methods with some debugging functionality. I can write the following: import scala.language.experimental.macros import scala.reflect.macros.Context object WrapperExample { def wrap[A](a: A): A = macro wrap_impl[A] def wrap_impl[A: c.WeakTypeTag](c:

Using LabelDef in scala macros (2.10)

纵饮孤独 提交于 2019-12-21 04:59:21
问题 I'm experimenting with the scala 2.10 macro features. I have trouble using LabelDef in some cases, though. To some extent I peeked in the compiler's code, read excerpts of Miguel Garcia's papers but I'm still stuck. If my understanding is correct , a pseudo-definition would be: LabelDef(labelName, listOfParameters, stmsAndApply) where the 3 arguments are Trees and: - labelName is the identifier of the label $L being defined - listOfParameters correspond to the arguments passed when label-

Why sbt compile doesn't copy unmanaged resources to classpath?

感情迁移 提交于 2019-12-21 03:53:34
问题 Could you tell me why sbt compile doesn't copy unmanaged resources to classpath? On the other hand sbt package does. As result I can't start debugging unless I invoke package call manually :( I'm using SBT 0.12.1 Below is my build.sbt. import AssemblyKeys._ // put this at the top of the file net.virtualvoid.sbt.graph.Plugin.graphSettings assemblySettings organization := "com.zzz" version := "0.1" scalaVersion := "2.10.2" scalacOptions := Seq("-unchecked", "-language:reflectiveCalls,postfixOps

In Scala 2.10, how do you create a ClassTag given a TypeTag

谁说我不能喝 提交于 2019-12-21 03:52:07
问题 I'd like to define a function that returns an Array, and I have a TypeTag. Can I generate the required ClassTag? scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ scala> def fun[X: TypeTag]: Array[X] = Array.ofDim[X](10) <console>:11: error: No ClassTag available for X def fun[X: TypeTag]: Array[X] = Array.ofDim[X](10) Or is it necessary to provide implicit evidence of the ClassTag: scala> import reflect.ClassTag import reflect.ClassTag scala> def fun[X:

How to disambiguate links to methods in scaladoc?

和自甴很熟 提交于 2019-12-20 11:05:49
问题 I'm documenting a Scala class with overloaded methods. How can I distinguish them when referring to them in scaladoc comments? For example, if I have /** * The most important method is [[Doc.foo]]. */ object Doc { def foo[A]: A = throw new UnsupportedOperationException; def foo[A,B >: A](x: A): B = x; } and run sbt doc I get Doc.scala:1: warning: The link target "Doc.foo" is ambiguous. Several (possibly overloaded) members fit the target: method foo[A,B>:A](x:A):B in object Doc [chosen]

How do I eliminate type erasure warning?

旧街凉风 提交于 2019-12-20 05:01:13
问题 I have this code that gets the abstract type pattern reflect.runtime.universe.MethodSymbol is unchecked since it is eliminated by erasure warnings in every place that the case keyword is used. I have a feeling it's something strange about the reflection APIs. The thing is, it works when I run it (both passing and failing code paths) so it seems like the warning is erroneous. How do I eliminate the warning? import scala.reflect.runtime.{universe => u} val docs = { val ann = u.typeOf[T].members

How do I eliminate type erasure warning?

主宰稳场 提交于 2019-12-20 05:01:00
问题 I have this code that gets the abstract type pattern reflect.runtime.universe.MethodSymbol is unchecked since it is eliminated by erasure warnings in every place that the case keyword is used. I have a feeling it's something strange about the reflection APIs. The thing is, it works when I run it (both passing and failing code paths) so it seems like the warning is erroneous. How do I eliminate the warning? import scala.reflect.runtime.{universe => u} val docs = { val ann = u.typeOf[T].members

Macros: path dependent type inference confusion

青春壹個敷衍的年華 提交于 2019-12-19 09:41:42
问题 I tried to simplify the creation of ASTs, but got a weird error message: case class Box(i: Int) object M { import language.experimental.macros import scala.reflect.makro.Context case class meth(obj: String, method: String)(implicit val c: Context) { import c.universe._ def apply(xs: Tree*) = Apply(Select(Ident(obj), newTermName(method)), xs.toList) } def box(n: Int): Box = macro boxImpl def boxImpl(c: Context)(n: c.Expr[Int]): c.Expr[Box] = { import c.universe._ implicit val cc: c.type = c n

Macros: path dependent type inference confusion

元气小坏坏 提交于 2019-12-19 09:40:17
问题 I tried to simplify the creation of ASTs, but got a weird error message: case class Box(i: Int) object M { import language.experimental.macros import scala.reflect.makro.Context case class meth(obj: String, method: String)(implicit val c: Context) { import c.universe._ def apply(xs: Tree*) = Apply(Select(Ident(obj), newTermName(method)), xs.toList) } def box(n: Int): Box = macro boxImpl def boxImpl(c: Context)(n: c.Expr[Int]): c.Expr[Box] = { import c.universe._ implicit val cc: c.type = c n