scala-macros

When calling a scala function with compile-time macro, how to failover smoothly when it causes compilation errors?

一世执手 提交于 2021-02-13 05:43:30
问题 Assuming that I intend to use the singleton/literal type feature in a scala program, this feature is provided in shapeless library in scala 2.12 (scala 2.13 supports native literal type but let's use shapeless as an example) In shapeless, literal type is represented as a path-dependent inner type of Witness object, which can be implicitly converted from a scala literal/const: import com.tribbloids.spike.BaseSpec import shapeless.Witness import scala.util.Random val w: Witness.Lt[Int] = 3 val

How to get underlying constant type from singleton type with Scala reflection API

不想你离开。 提交于 2021-02-10 18:32:49
问题 import scala.reflect.runtime.universe._ val a: 42 = 42 val t: Type = typeOf[a.type] assert(getConstantType(t).get =:= typeOf[42]) def getConstantType(t: Type): Option[ConstantType] = ??? How could I generally implement getConstantType so that the above assertion passes? I assumed that something like this was possible since the assertion below passes: assert(t <:< typeOf[42]) t.widen goes too far as it return Int . I'm looking for something that returns Int(42) . 回答1: How about assert(t

How to get underlying constant type from singleton type with Scala reflection API

半腔热情 提交于 2021-02-10 18:32:48
问题 import scala.reflect.runtime.universe._ val a: 42 = 42 val t: Type = typeOf[a.type] assert(getConstantType(t).get =:= typeOf[42]) def getConstantType(t: Type): Option[ConstantType] = ??? How could I generally implement getConstantType so that the above assertion passes? I assumed that something like this was possible since the assertion below passes: assert(t <:< typeOf[42]) t.widen goes too far as it return Int . I'm looking for something that returns Int(42) . 回答1: How about assert(t

what should a scala implicit macro have to return to tell the compiler “forget my result, continue your search”

不羁的心 提交于 2021-02-10 12:42:39
问题 I have an implicit macro with a greedy signature implicit def materializeHelper[C <: Any]: Helper[C] = macro materializeHelperImpl[C] def materializeHelperImpl[C <: Any: ctx.WeakTypeTag](ctx: blackbox.Context): ctx.Expr[Helper[C]] = ??? According to it's signature it would materialize a Helper[C] for any C. But the body is much more picky. It only accepts C s which are sealed traits. What should the macro return to tell the compiler "forget my result, continue your implicit search as if I

Why value method cannot be used outside macros?

走远了吗. 提交于 2021-02-08 09:37:12
问题 The error message `value` can only be used within a task or setting macro, such as :=, +=, ++=, Def.task, or Def.setting. val x = version.value ^ clearly indicates how to fix the problem, for example, using := val x = settingKey[String]("") x := version.value The explanation in sbt uses macros heavily states The value method itself is in fact a macro, one that if you invoke it outside of the context of another macro, will result in a compile time error, the exact error message being... And

In a scala macro, how to get the full name that a class will have at runtime?

隐身守侯 提交于 2021-01-29 22:08:03
问题 The intention is to get at run-time some info of particular classes that is available only at compile-time. My approach was to generate the info at compile time with a macro that expands to a map that contains the info indexed by the runtime class name. Something like this: object macros { def subClassesOf[T]: Map[String, Info] = macro subClassesOfImpl[T]; def subClassesOfImpl[T: ctx.WeakTypeTag](ctx: blackbox.Context): ctx.Expr[Map[String, Info]] = { import ctx.universe._ val classSymbol =

scala AST Select node can't find members inherited from parent

女生的网名这么多〃 提交于 2021-01-29 19:25:05
问题 I'm writing a macro called assign whose job is to assign values from members of one instance to another with a particular prefix added to the name of the member. For example, I have an instance with members named my_prefix_data, my_prefix_rden, ... and I want to assign values to these members from another instance with corresponding members named data, rden, ... . I've made a prototype version of the macro that just handles the my_prefix_data <- data assignment. The assignments are to be made

How to overload generic method with different evidence without ambiguity?

浪尽此生 提交于 2021-01-29 17:38:50
问题 I have a customized compare methods that takes two parameters. One of them are expected to be implicitly convertible to another: object Test extends App { def compare[T1, T2](a: T1, b: T2)(implicit ev: T1 => T2) = compareImpl[T2](ev(a), b) def compare[T1, T2](a: T1, b: T2)(implicit ev: T2 => T1) = compareImpl[T1](a, ev(b)) def compareImpl[T](a: T, b: T) = a == b case class Foo(s: String) case class Bar(s: String) implicit def foo2bar(f: Foo): Bar = Bar(f.s) println(compare(Foo("hello"), Bar(

Def Macro, pass parameter from a value

若如初见. 提交于 2021-01-29 13:52:21
问题 I have a working macros ie: object Main extends App { println("Testing assert macro...") val result = Asserts.assert(false, "abc") } and import scala.reflect.macros.blackbox.Context import scala.language.experimental.macros object Asserts { val assertionsEnabled: Boolean = true def assert(cond: Boolean, msg: String): Unit = macro assertImpl def assertImpl(c: Context)(cond: c.Expr[Boolean], msg: c.Expr[String]) : c.Expr[Unit] = { import c.universe._ cond.tree match { case Literal(Constant(cond

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

瘦欲@ 提交于 2021-01-20 13:08:23
问题 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])