scala-macros

Dependent type seems to “not work” when generated by Scala macro

╄→尐↘猪︶ㄣ 提交于 2019-12-05 18:27:17
Apologies for the handwavey title. I’m not entirely sure how to phrase the question succinctly, since I’ve never encountered something like this before. Background info: I have the following trait, where the type U is meant to hold a Shapeless extensible record type: trait Flattened[T] { type U <: shapeless.HList def fields: U } I’m using a blackbox macro (for reasons outside the scope of this question) to create new instances of the trait: object flatten { import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context def apply[T](struct: T): Flattened[T] = macro impl

create an ambiguous low priority implicit

坚强是说给别人听的谎言 提交于 2019-12-05 16:47:07
问题 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

Macro to access source code of function at runtime

三世轮回 提交于 2019-12-05 12:37:36
Using Scala macros I would like to get access to source code of function f. Here is simplified example of my problem: def logFImplementation(f: => Boolean) { val sourceCodeOfF: String = ... // <-- how to get source code of f?? println("Scala source of f=" + sourceCodeOfF) } so for: logFImplementation { val i = 5; List(1, 2, 3); true } it should print: Scala source of f=val i: Int = 5; immutable.this.List.apply[Int](1, 2, 3); true (Right now I tested Macro to access source code text at runtime and it works great for { val i = 5; List(1, 2, 3); true }.logValueImpl but for f.logValueImpl it just

Way to enhance a class with function delegation

独自空忆成欢 提交于 2019-12-05 10:06:53
I have the following classes in Scala: class A { def doSomething() = ??? def doOtherThing() = ??? } class B { val a: A // need to enhance the class with both two functions doSomething() and doOtherThing() that delegates to A // def doSomething() = a.toDomething() // def doOtherThing() = a.doOtherThing() } I need a way to enhance at compile time class B with the same function signatures as A that simply delegate to A when invoked on B. Is there a nice way to do this in Scala? Thank you. In Dotty (and in future Scala 3), it's now available simply as class B { val a: A export a } Or export a.

How to get Scala function's parameters / return type?

强颜欢笑 提交于 2019-12-05 07:57:18
I have a function, and would like to obtain its parameter types and return type for use in Scala macros. scala> val fn = (a: String, b: Double) => 123 fn: (String, Double) => Int = <function2> scala> fn.getClass res1: Class[_ <: (String, Double) => Int] = class $anonfun$1 In the above example, the parameter types and return type already get printed at both lines, but I don't know how to access them. Even with toString I'd be stuck with the <function2> and class $anonfun$1 parts right of the = sign -- otherwise a bit of ugly string parsing might have done. I found that the MethodSymbolApi

how to print variable name and value using a scala macro?

ぃ、小莉子 提交于 2019-12-05 07:29:58
I am sure there is a more elegant way of writing the following macro which prints the name and value of a variable: def mprintx(c: Context)(linecode: c.Expr[Any]): c.Expr[Unit] = { import c.universe._ val namez = (c.enclosingImpl match { case ClassDef(mods, name, tparams, impl) => c.universe.reify(c.literal(name.toString).splice) case ModuleDef(mods, name, impl) => c.universe.reify(c.literal(name.toString).splice) case _ => c.abort(c.enclosingPosition, "NoEnclosingClass") }).toString match { case r_name(n) => n case _ => "Unknown?" } val msg = linecode.tree.productIterator.toList.last.toString

How to make IntelliJ IDEA recognise code created by macros?

半城伤御伤魂 提交于 2019-12-05 06:42:55
Background I have an sbt-managed Scala project that uses the usual sbt project layout for Scala projects with macros, i.e., a subproject that contains the macros a main project that is the actual application and that depends on the macro subproject. The macros are macro annotations which, in essence, generate companion objects for regular classes. The generated companion objects declare, amongst other members, apply/unapply methods. I used the sbt-idea plugin to generate a corresponding IntelliJ IDEA project, and I use the sbt console from IDEA's sbt-plugin to compile and run my Scala

How can parameters/settings be passed to a Scala macro?

╄→гoц情女王★ 提交于 2019-12-05 01:52:39
How can parameters/settings be passed to a Scala macro? These settings should not be global, but per macro invocation. What I would like to have is something similar to this: def a(param: Int) = macro internalMacro("setting 1") def b(param: Int) = macro internalMacro("setting 2") whereas setting 1 and setting 2 should then be constant values, accessible from within the macro, so I can make the internal behavior dependent on them. The parameter lists of your method and the macro definition have to line up exactly, with the macro definition method having an extra initial parameter list for the

Is it possible to write a scala macro whose returntype depends on argument?

蹲街弑〆低调 提交于 2019-12-04 18:59:31
For a DSL I would like to be able to do something like: object Creator { def create[T](s :String) :Foo[T] = macro createImpl[T] def createImpl[T](c :Context)(s :c.Expr[String]) : c.Expr[Foo[T]] = { reify(new Foo[Any]()) } } My problem is to replace the Any in reify with something that will return the correctly parametrized version. (above I use a string argument, but in the final version I plan to use the companion object of class T as a marker to know the argument-type of a Function1[T,Unit]) You need to: a) write new Foo[T]() instead of new Foo[Any]() (easy) b) pass in the macro a

How to debug a macro annotation?

 ̄綄美尐妖づ 提交于 2019-12-04 13:24:09
问题 Background I'm trying to add a @Prisms annotation to Monocle that will work as follows. Given: @Prisms sealed trait Foo case object A extends Foo case object B extends Foo it will generate a companion object for Foo : object Foo { val a = monocle.macros.GenPrism.apply[Foo, A] val b = monocle.macros.GenPrism.apply[Foo, B] } (or if the companion object already exists, it will add those methods to it). The macro relies on directKnownSubclasses to find A and B , so there will be a note in the