scala-macros

Scala Macro: get param default value

邮差的信 提交于 2019-12-04 12:42:50
I have the next code, and i would like to extract the default parametr from value. // def extractor[T] = macro extractorImpl[T] def extractorImpl[T: c.WeakTypeTag](c: Context) = { //first i got a type contructor ??? } i try with attachments but attachments.all return a Set[Any] with (for example) SymbolSourceAttachment(val name: String = "new name") SymbolSourceAttachment contain ValDef but i do not know how to extract from SymbolSourceAttachment ValDef . By the way i should to get a Map[String, String]("name" -> "new name") Example: case class Person(name: String = "new name") object Macro {

Scala Macros: Getting a List of TypeSymbols to be used at runtime

依然范特西╮ 提交于 2019-12-04 12:26:29
问题 Is there a way to return a List of TypeSymbol s for each class under a package using macros? What I am trying to achieve is to write a macro that gives out something equivalent to this list: scala> import scala.reflect.runtime.universe._ import scala.reflect.runtime.universe._ scala> case class MyClass1() defined class MyClass1 scala> case class MyClass2() defined class MyClass2 scala> val typeSymbols = List(typeOf[MyClass1].typeSymbol, typeOf[MyClass2].typeSymbol) typeSymbols: List[reflect

scala 2.10.2 calling a 'macro method' with generic type not work

こ雲淡風輕ζ 提交于 2019-12-04 11:18:30
问题 I define following macro to transform case fields to map import scala.language.experimental.macros import scala.reflect.macros.Context def asMap_impl[T: c.WeakTypeTag](c: Context)(t: c.Expr[T]) = { import c.universe._ val mapApply = Select(reify(Map).tree, newTermName("apply")) val pairs = weakTypeOf[T].declarations.collect { case m: MethodSymbol if m.isCaseAccessor => val name = c.literal(m.name.decoded) val value = c.Expr(Select(t.tree, m.name)) reify(name.splice -> value.splice).tree } c

How to build a dynamic sequence in a scala macro?

爱⌒轻易说出口 提交于 2019-12-04 09:48:17
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 cast (how can i fix that?). Where I am stuck is when I want the macro to output some case class which

Instantiate a class with Scala Macro or reflection

非 Y 不嫁゛ 提交于 2019-12-04 05:03:08
On my scala code, I want to be able to instantiate a new class. For instance, supose I have the code below: class Foo { def foo=10 } trait Bar { val bar=20 } Ideally, I want to be able to do something like: def newInstance[A <: Foo] = { new A with Bar } newInstance[Foo] But, of course this doesn't work. I tried to use reflection to instantiate a class, but it seems that I'm only able to instantiate a new class (and not mix-in with a trait). I think it would be possible to make this work using Macros, but I'm not sure even where to start. What I'm trying to do is like the following Ruby code:

create an ambiguous low priority implicit

泪湿孤枕 提交于 2019-12-04 02:23:11
Consider the default codec as offered in the io package. implicitly[io.Codec].name //res0: String = UTF-8 It's a "low priority" implicit so it's easy to override without ambiguity. implicit val betterCodec: io.Codec = io.Codec("US-ASCII") implicitly[io.Codec].name //res1: String = US-ASCII It's also easy to raise its priority level. import io.Codec.fallbackSystemCodec implicit val betterCodec: io.Codec = io.Codec("US-ASCII") implicitly[io.Codec].name //won't compile: ambiguous implicit values But can we go in the opposite direction? Can we create a low level implicit that disables ("ambiguates

Scala Macros: Checking for a certain annotation

家住魔仙堡 提交于 2019-12-04 01:49:54
Thanks to the answers to my previous question , I was able to create a function macro such that it returns a Map that maps each field name to its value of a class, e.g. ... trait Model case class User (name: String, age: Int, posts: List[String]) extends Model { val numPosts: Int = posts.length ... def foo = "bar" ... } So this command val myUser = User("Foo", 25, List("Lorem", "Ipsum")) myUser.asMap returns Map("name" -> "Foo", "age" -> 25, "posts" -> List("Lorem", "Ipsum"), "numPosts" -> 2) This is where Tuple s for the Map are generated (see Travis Brown's answer ): ... val pairs =

Scala macro - Infer implicit value using `c.prefix`

倾然丶 夕夏残阳落幕 提交于 2019-12-03 17:02:55
c.inferImplicitValue infers implicit values in the call site scope. Is it possible to infer implicits using the c.prefix scope? This is not valid code, but expresses what I need: c.prefix.inferImplicitValue I'm currently using a naive implementation for this purpose[1], but it has some limitations like not inferring implicit values from def s and detecting duplicated/ambiguous implicit values. [1] https://github.com/getquill/quill/blob/9a28d4e6c901d3fa07e7d5838e2f4c1f3c16732b/quill-core/src/main/scala/io/getquill/util/InferImplicitValueWithFallback.scala#L12 Simply generating a block with an

Using LabelDef in scala macros (2.10)

 ̄綄美尐妖づ 提交于 2019-12-03 16:08:51
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- apply occurs, as in $L(a1,...,an) , and can be empty - stmsAndApply corresponds to the block of statements

Using attachments with macros in Scala 2.10

若如初见. 提交于 2019-12-03 14:42:27
Update: I suspect that what I want might not be possible, and I've written up a blog post with my reasoning (and some alternatives) here . I'd be very happy to be told that I'm wrong. Suppose I want to create a instance of a trait using a factory method with a macro implementation. This method will take as an argument a path to a resource that the macro will read and parse (at compile time) into a map from strings to strings. That part is all fairly straightforward. Now suppose I want to associate the resulting map with the instance I'm creating so that I can use it in subsequent macro calls