scala-macros

Get the module symbol, given I have the module class, scala macro

。_饼干妹妹 提交于 2021-01-20 13:02:02
问题 I'm trying to build a simple typeclass IsEnum[T] using a macro. I use knownDirectSubclasses to get all the direct subclasses if T , ensure T is a sealed trait, and that all subclasses are of case objects (using subSymbol.asClass.isModuleClass && subSymbol.asClass.isCaseClass ). Now I'm trying to build a Seq with the case objects referred by the subclasses. It's working, using a workaround: Ident(subSymbol.asInstanceOf[scala.reflect.internal.Symbols#Symbol].sourceModule.asInstanceOf[Symbol])

Get the module symbol, given I have the module class, scala macro

寵の児 提交于 2021-01-20 13:01:09
问题 I'm trying to build a simple typeclass IsEnum[T] using a macro. I use knownDirectSubclasses to get all the direct subclasses if T , ensure T is a sealed trait, and that all subclasses are of case objects (using subSymbol.asClass.isModuleClass && subSymbol.asClass.isCaseClass ). Now I'm trying to build a Seq with the case objects referred by the subclasses. It's working, using a workaround: Ident(subSymbol.asInstanceOf[scala.reflect.internal.Symbols#Symbol].sourceModule.asInstanceOf[Symbol])

Get the module symbol, given I have the module class, scala macro

帅比萌擦擦* 提交于 2021-01-20 13:00:43
问题 I'm trying to build a simple typeclass IsEnum[T] using a macro. I use knownDirectSubclasses to get all the direct subclasses if T , ensure T is a sealed trait, and that all subclasses are of case objects (using subSymbol.asClass.isModuleClass && subSymbol.asClass.isCaseClass ). Now I'm trying to build a Seq with the case objects referred by the subclasses. It's working, using a workaround: Ident(subSymbol.asInstanceOf[scala.reflect.internal.Symbols#Symbol].sourceModule.asInstanceOf[Symbol])

Scala conditional compilation

自古美人都是妖i 提交于 2021-01-20 12:23:06
问题 I'm writing a Scala program and I want it to work with two version of a big library. This big library's version 2 changes the API very slightly (only one class constructor signature has an extra parameter). // Lib v1 class APIClass(a: String, b:Integer){ ... } // Lib v2 class APIClass(a: String, b: Integer, c: String){ ... } // And my code extends APIClass.. And I have no #IFDEF class MyClass() extends APIClass("x", 1){ // <-- would be APIClass("x", 1, "y") in library v2 ... } I really don't

Macro expansion contains free variable

白昼怎懂夜的黑 提交于 2021-01-06 07:25:40
问题 My code compiles with the following error: Macro expansion contains free term variable Hello ... I have reduced it to minimal example: class Hello(val hi: String) { val xx = reify(hi) var yy = q"" } def setYYImpl(c: Context)(hExpr: c.Expr[Hello]): c.Expr[Hello] = { import c.universe._ val hello = c.eval(c.Expr[Hello](c.untypecheck(hExpr.tree.duplicate))) val xxVal = c.internal.createImporter(u).importTree(hello.xx.tree) c.Expr(q"""{val h = new Hello("HO"); h.yy=$xxVal; h}""") // it should set

Macro expansion contains free variable

余生颓废 提交于 2021-01-06 07:24:09
问题 My code compiles with the following error: Macro expansion contains free term variable Hello ... I have reduced it to minimal example: class Hello(val hi: String) { val xx = reify(hi) var yy = q"" } def setYYImpl(c: Context)(hExpr: c.Expr[Hello]): c.Expr[Hello] = { import c.universe._ val hello = c.eval(c.Expr[Hello](c.untypecheck(hExpr.tree.duplicate))) val xxVal = c.internal.createImporter(u).importTree(hello.xx.tree) c.Expr(q"""{val h = new Hello("HO"); h.yy=$xxVal; h}""") // it should set

Wrap function implementations returning a specific type into another function programatically

北战南征 提交于 2020-12-29 08:19:07
问题 I would like to wrap all the user defined functions in a scala project that return a certain type T , into a function that accepts a T and the function name as parameters. eg. given this function is in scope: def withMetrics[T](functionName: String)(f: => Try[T]): Try[T] = { f match { case _: Success[T] => println(s"send metric: success for $functionName") case _: Failure[T] => println(s"send metric: failure for $functionName") } f } the user can send metrics for their functions which return

Wrap function implementations returning a specific type into another function programatically

℡╲_俬逩灬. 提交于 2020-12-29 08:11:29
问题 I would like to wrap all the user defined functions in a scala project that return a certain type T , into a function that accepts a T and the function name as parameters. eg. given this function is in scope: def withMetrics[T](functionName: String)(f: => Try[T]): Try[T] = { f match { case _: Success[T] => println(s"send metric: success for $functionName") case _: Failure[T] => println(s"send metric: failure for $functionName") } f } the user can send metrics for their functions which return

Wrap function implementations returning a specific type into another function programatically

℡╲_俬逩灬. 提交于 2020-12-29 08:07:32
问题 I would like to wrap all the user defined functions in a scala project that return a certain type T , into a function that accepts a T and the function name as parameters. eg. given this function is in scope: def withMetrics[T](functionName: String)(f: => Try[T]): Try[T] = { f match { case _: Success[T] => println(s"send metric: success for $functionName") case _: Failure[T] => println(s"send metric: failure for $functionName") } f } the user can send metrics for their functions which return

Implicit macro. Default implicit value. How?

吃可爱长大的小学妹 提交于 2020-12-15 07:08:32
问题 I don't even know how to ask the question. I have a macro that creates an instance of IsEnum[T] for a type T . I'm doing testing for it, and want to make sure that the implicit is not found for types that are not sealed, or that, in general, don't meet the requirements of an enum. So I created this method for testing def enumOf[T](implicit isEnum:IsEnum[T] = null) = isEnum And then I ensure that enumOf[NotAnEnum] == null But instead, it fails at compile time. One thing is the macro erroring.