scala-macros

What is the supertype of all functions in Scala?

ε祈祈猫儿з 提交于 2020-01-04 02:44:14
问题 I know I can do instanceOf checks against Function1 or Function2 etc but is there a generic way to see if something is function or not (it can have arbitray number of args). I tried defining something like this: type FuncType = (Any*) -> Any But that did not work either. Basically I have some code that looks like this: call = (name: Any, args: Any*) -> if name.isFunction then name.castAs[Function].apply(args) else name aFunction = (name: String) => "Hello " + name notAFunction = "Hello rick"

Infer a type of a tree in a scala macro

淺唱寂寞╮ 提交于 2020-01-03 17:48:03
问题 Inside a macro, how can I ask the compiler to infer the type of a constructed tree? I've only found Context.typeCheck, but that only checks the types but doesn't return the result. 回答1: If you've type-checked the tree you can just use its tpe method: scala> def impl(c: Context) = c.literal(c.typeCheck(c parse "1+1").tpe.toString) impl: (c: scala.reflect.macros.Context)c.Expr[String] scala> def mac = macro impl mac: String scala> println(mac) Int(2) You could also wrap it in an expression, of

“macro implementation reference has wrong shape” in the Scala Documentation examples

孤者浪人 提交于 2020-01-03 16:48:25
问题 The following macro is pasted from http://docs.scala-lang.org/overviews/quasiquotes/usecases.html: import reflect.macros.Context import language.experimental.macros val universe = reflect.runtime.universe; import universe._ import reflect.runtime.currentMirror import tools.reflect.ToolBox val toolbox = currentMirror.mkToolBox() object debug { def apply[T](x: =>T): T = macro impl def impl(c: Context)(x: c.Tree) = { import c.universe._ val q"..$stats" = x val loggedStats = stats.flatMap { stat

“macro implementation reference has wrong shape” in the Scala Documentation examples

独自空忆成欢 提交于 2020-01-03 16:43:33
问题 The following macro is pasted from http://docs.scala-lang.org/overviews/quasiquotes/usecases.html: import reflect.macros.Context import language.experimental.macros val universe = reflect.runtime.universe; import universe._ import reflect.runtime.currentMirror import tools.reflect.ToolBox val toolbox = currentMirror.mkToolBox() object debug { def apply[T](x: =>T): T = macro impl def impl(c: Context)(x: c.Tree) = { import c.universe._ val q"..$stats" = x val loggedStats = stats.flatMap { stat

How can I apply a macro annotation to a case class with a context bound?

时间秒杀一切 提交于 2020-01-03 13:58:06
问题 When I try to add a macro annotation to my case class: @macid case class CC[A: T](val x: A) I get the error: private[this] not allowed for case class parameters @macid is just the identity function, defined as a whitebox StaticAnnotation: import scala.language.experimental.macros import scala.reflect.macros.whitebox.Context import scala.annotation.StaticAnnotation class macid extends StaticAnnotation { def macroTransform(annottees: Any*): Any = macro macidMacro.impl } object macidMacro { def

How can I apply a macro annotation to a case class with a context bound?

拈花ヽ惹草 提交于 2020-01-03 13:57:06
问题 When I try to add a macro annotation to my case class: @macid case class CC[A: T](val x: A) I get the error: private[this] not allowed for case class parameters @macid is just the identity function, defined as a whitebox StaticAnnotation: import scala.language.experimental.macros import scala.reflect.macros.whitebox.Context import scala.annotation.StaticAnnotation class macid extends StaticAnnotation { def macroTransform(annottees: Any*): Any = macro macidMacro.impl } object macidMacro { def

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

烈酒焚心 提交于 2020-01-01 05:39:26
问题 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

annotation macro that rewrites and impls a trait, generics not processed correctly

孤者浪人 提交于 2019-12-31 05:31:05
问题 I am writing a macro that needs to create a class that rewrites a trait, having the same methods/args of the trait but different return type. So say we got: trait MyTrait[T] { def x(t1: T)(t2: T): T } @AnnProxy class MyClass[T] extends MyTrait[T] MyClass will be rewritten to: class MyClass[T] { def x(t1: T)(t2: T): R[T] } (so x will now return R[T] instead of T) I wrote the macro and debugging it, it produces this code: Expr[Any](class MyClass[T] extends scala.AnyRef { def <init>() = { super.

Is it possible to generate Apply from WeakTypeTag inside a scala macro?

女生的网名这么多〃 提交于 2019-12-30 18:59:50
问题 I have a WeakTypeTag of some type in my macro, and I want to generate code as follows: macroCreate[SomeObject] // => SomeObject(1) The definition of a macro will be something like this: def macroCreate[A] = macro _macroCreate[A] def _macroCreate[A](c: Context)(implicit wtt: c.WeakTypeTag[A]) = { c.Expr(Apply(Select(???, newTermName("apply")), List(c.literal(1).tree))) } The problem is, how do I get Select for the given type? I can use a workaround of converting the type to string, splitting

Do macros make naturally chained comparisons possible in Scala?

混江龙づ霸主 提交于 2019-12-24 00:26:00
问题 Scala does not provide chained comparisons as Python does: // Python: 0 < x <= 3 // Scala: 0 < x && x <= 3 Will Scala 2.10 with the new macro feature enable the programmer write a library that adds this feature? Or is this beyond the scope of Scala's macros? Macros seem to be the right choice for the implementation of such syntactic sugar as they do not complicate the parser/compiler. 回答1: You don't need macros for it: class ChainedComparisons[T : Ordering](val res: Boolean, right: T) { def <