scala-macros

How to Typecheck a DefDef

笑着哭i 提交于 2019-12-12 11:47:06
问题 Within an annotation Macro, I'm enumerating members of a class and want the types of the methods that I find. So I happily iterate over the body of the class, and collect all the DefDef members. ... which I can't typecheck. For each DefDef I've tried wrapping it in an Expr and using actualType . I've tried duplicating the thing and transplanting it into an ad-hoc class (via quasiquotes). I've tried everything else I can think of :) The best I can get is either NoType or Any , depending on the

Strange type mismatch with a macro: found: singleton type with underlying type A, required: A

主宰稳场 提交于 2019-12-12 11:34:30
问题 I have class Foo[A] { def foo[B](x: A, y: B) = y } class Bar[A] extends Foo[A] { override def foo[B](x: A, y: B) = superCall } where superCall whitebox macro should expand to super.foo[B](x, y) , and that's what -Ymacro-debug-lite shows. The problem is that it fails to compile with following error: [error] /home/aromanov/IdeaProjects/scala-dry/src/test/scala/com/github/alexeyr/scaladry/SuperTests.scala:80: type mismatch; [error] found : y.type (with underlying type B) [error] required: B

Get name of defining val

筅森魡賤 提交于 2019-12-12 10:48:49
问题 I would like to catch the name of variable that output of my macro is assigned to. Exactly like project in build.sbt. I would prefer out of the box solution(library) if there is one, because it looks like pretty general use case. Here is small example val someValue = myMacro() and as an output of myMacro() I would like to get string "someValue" . 回答1: You are looking for SourceCode. Example: scala> def myName(implicit name: sourcecode.Name) = name.value myName: (implicit name: sourcecode.Name

Quasiquotes for multiple parameters and parameter lists

心已入冬 提交于 2019-12-12 08:38:44
问题 Quasiquotes are amazing—they make writing macros in Scala hugely less painful, and in my experience they almost always just work exactly as I'd expect. And best of all, they're now available as a plugin in Scala 2.10. This question is about a small problem that I ran into while writing this blog post. It's on my list of stuff to look into when I can find a few minutes, but I figured I'd post it here in case someone else can beat me to it, and to help other people who are running into the same

Obtaining a WeakTypeTag for a given type in a Scala macro annotation

为君一笑 提交于 2019-12-12 03:25:33
问题 I'm writing a Scala macro annotation @model used to annotate my case classes and which automatically adds some metadata to the companion object of the annotated class, based on the fields of the annotated case class. I would like to obtain more information about the type of the case class's parameters, especially, check if they implement a certain trait. I thought obtaining a WeakTypeTag for them was the way to go, but I can't seem to get one the way they are obtained in def macros.

Scala macros Type or Symbol lifted

倾然丶 夕夏残阳落幕 提交于 2019-12-12 02:49:06
问题 I'm trying to use the fields of a type in a macro, and pass the symbol or typesigs for the fields to a method so I can do some operations that require concrete information about the type. I have code like this (played about with variations): object Macros { import scala.reflect.runtime.universe._ def foo(t: Symbol) : String = t.name.decoded def materializeWriterImpl[T: c.WeakTypeTag](c: Context): c.Expr[List[String]] = { import c.universe._ val tpe = weakTypeOf[T] val fields = tpe

How does one use quasiquotes to obtain the type of a value?

风流意气都作罢 提交于 2019-12-11 14:08:59
问题 I’m trying to write the following: val value: Int = 3 val tpe = typeOf(value) // This is pseudocode. What should // actually go on this line? q""" type U = $tpe val v: U = $value """ Essentially, I need to capture the type of value ( Int ) in tpe and assign it to U . How does one do this? 回答1: Try import scala.reflect.runtime.universe._ def getType[A: TypeTag](a: A): Type = typeOf[A] val value: Int = 3 val tpe = getType(value) // Int Connected question: How does one obtain the type of the

Using scala macro from java

那年仲夏 提交于 2019-12-11 08:09:08
问题 Currently I'm writing a scala macro library, and I would like to make it usable from java too. But I cannot think of a way to access scala macro methods form java source code. Is there any way we can use scala macro method from java. 回答1: Scala macros are instructions for scala compiler, java code is compiled by javac, which has no idea about both scala and scala macros, so I guess the answer is: there is no way to use them from java. 来源: https://stackoverflow.com/questions/25078538/using

Is there a way to guarantee case class copy methods exist with type classes in Scala?

心已入冬 提交于 2019-12-11 07:58:13
问题 In the example below I have a type class Foo , and would like to somehow guarantee that all members conforming to Foo (such as Bar via barFoo ) have a copy method such as the one generated by way of being a case class . I haven't thought of a way to do this. In this case the copy signature would be possibly be something like copy(foo: F, aa: List[T] = foo.aa, maybe: Option[T] = foo.maybe): F . trait Foo[F] { type T def aa(foo: F): List[T] def maybe(foo: F): Option[T] } final case class Bar(aa

Proper way to pattern match the value of a TypeTree from a ValDef in Scala Macros?

北城余情 提交于 2019-12-11 07:13:46
问题 I need to read the fields of a case class and do different things depending on the field's type. I thought I'd try with a macro, reading the ValDefs and pattern matching on the TypeTree of each, but that doesn't reveal what each TypeTree represents (e.g. Ints and Strings both appear as TypeTrees). Is there an alternative to calling typeTree.toString and matching on the values of the Strings (e.g. "String" or "Int")? 回答1: You can use TypeTree's tpe method to see the underlying type. 来源: https: