FAQ Database Discussion Community


Least common TypeTag from two tags in Scala?

scala,scala-macros
Scala's TypeTags are relatively easy to compare, and capture - but does Scala offer any composition function that act on 2 tags? For example, I'm working with tags in a very generalized way (meaning, type T is gone). Can I request Scala to provide me a TypeTag of the least...

Using Macro to Make Case Class

scala,scala-macros
Given the following macro (thanks @TravisBrown for this help ): JetDim.scala case class JetDim(dimension: Int) { require(dimension > 0) } object JetDim { def validate(dimension: Int): Int = macro JetDimMacro.apply def build(dimension: Int): JetDim = JetDim(validate(dimension)) } JetDimMacro.scala import reflect.macros.Context object JetDimMacro { sealed trait PosIntCheckResult case class LteqZero(x: Int)...

scala macro - how to expand an Array[TermName] to HList concatenation

scala,scala-macros
Suppose I have a case class case class(id: Option[Long], name: String, gender: String) Then I got the field names via compile-time reflect val names = ... // Array(TermName(id), TermName(name), TermName("gender")) How could I transform those names to id :: name :: gender :: HNil (which has type Option[Long] :: String...

Compiler uses structural types for case class generated inside a whitebox macro

scala,scala-macros
The following macro generates a case class Person and returns an instance of this class. It uses whitebox macros so the compiler can infer the type Person. This allows the macro client to call p.name even though this field was generated inside the macro. import scala.language.experimental.macros class MacroDefs(val c: scala.reflect.macros.whitebox.Context)...

Scala recursive macro?

scala,scala-macros,lenses,scala-macro-paradise,scala-meta
I was wondering whether Scala supports recursive macro expansion e.g. I am trying to write a lens library with a lensing macro that does this: case class C(d: Int) case class B(c: C) case class A(b: B) val a = A(B(C(10)) val aa = lens(a)(_.b.c.d)(_ + 12) assert(aa.b.c.d == 22)...

scala macros generating implicits

scala,macros,implicit,scala-macros
I am trying to generate some implicits via a macro -the condensed version of the macro looks like this: object Implicits { def generate(c:Context):c.Expr[Unit]={ import c.universe._ c.Expr[Unit] { q""" object Dud{ implicit val p:java.io.File = new java.io.File("/tmp") def toString():String ={ "Dud here" } } import Dud._ """ } } }...

How to check if some T is a case class at compile time in Scala?

scala,scala-macros,scala-2.11
I have the following macro: package macros import scala.reflect.macros.blackbox.Context object CompileTimeAssertions { def mustBeCaseClass[T]: Unit = macro CompileTimeAssertionsImpl.mustBeCaseClass[T] } object CompileTimeAssertionsImpl { def mustBeCaseClass[T: c.WeakTypeTag](c: Context): c.Expr[Unit] = { import c.universe._ val symbol = c.weakTypeTag[T].tpe.typeSymbol if (!symbol.isClass || !symbol.asClass.isCaseClass) { c.error(c.enclosingPosition, s"${symbol.fullName} must be a case class") } reify(Unit) }...

Type of q“null” is Literal, so invalid for return type Expr[X]

scala,scala-macros
I have a function def grammar(block: => Any): Grammar = macro grammarImpl with macro implementation def grammarImpl(c: Context)(block: c.Tree): c.Expr[Grammar] = { q"null" } But I get an error, type mismatch; found : c.universe.Literal required: c.Expr[Grammar] (which expands to) c.universe.Expr[Grammar] Replacing q"null" with c.literalNull however compiles fine, am I using...

“too few argument lists for macro invocation”

scala,scala-macros
Given the following code: case class JetDim(dimension: Int) { require(dimension > 0) } object JetDim { def build(dimension: Int): Int = macro JetDimMacro.apply } and the macro that it calls: def apply(dimension: Int): Int = macro applyImpl def applyImpl(c: Context)(dimension: c.Expr[Int]): c.Expr[Int] = ... I'm getting this compile-time error: [error]...

Communicate between two macros

scala,scala-macros
Is it possible to verify with macro foo that macro bar was called twice? In more details: trait Foo { foo() } class Bar extends Foo { bar() bar() } I would like it to compile only if bar() was called twice....

Can this be done with Scala macros?

scala,scala-macros
I wonder if this can be done with scala macros Consider this method: def doSomething(filter: Item => Boolean) = ... class Item(properties: Map[String, String]) extends Dynamic { def selectDynamic(name:String): String = { properties.getOrElse(name, "") } def updateDynamic(name: String)(value: String): Unit = { addProperty(name, value) } } And this usage doSomething(x=>...

How to use macros for concise, type-safe, unboxed enum?

scala,enums,metaprogramming,value-type,scala-macros
I am learning Scala macros and thinking of this as an exercise. Is it possible to use Scala macros to write down something like this (maybe not exactly this concrete syntax, but something without boilerplate) enum DayOfWeek = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday |...

Introducing variables named by user in a macro

scala,macros,scala-macros
So I want to define a macro that introduces a couple of variables as variable declarations such that the names are defined by the user of the macro. Say that I want to be able to write something like the following. foldOver(0)(hd, rest)(List(1, 2, 3)) { hd + rest }...

Can we create a type alias for a tuple dynamically?

scala,scala-macros,shapeless
Suppose I have a tuple val myTuple: (String,Int,String,...,Boolean) = ("",0,"",..,true) I can write a type alias type MyType = (String,Int,String,...,Boolean) val myTuple: MyType = ("",0,"",..,true) Can I write this type alias dynamically? Is there a way to not be explicit in this type aliasing and let the compiler find the...

Is there a good way to determine the package of a type in a Scala macro?

scala,macros,scala-macros
I'm writing a macro that needs to determine the package of the type it's parameterised on. It's possible with something like: def macroImpl[T: c.WeakTypeTag](c: Context) = { import c.universe._ val typ = weakTypeOf[T] val pkg = typ.typeSymbol.fullName.stripSuffix(s".${typ.typeSymbol.name}") } but this feels distinctly hacky. Is there a better approach?...

How to substitute actual type arguments in a generic method to obtain final types of its value arguments?

scala,generics,scala-macros
I have a macro with a signature like def generateSomething[A] = macro ... That is, it accepts a type parameter. The type is expected to be a case class and so it always has corresponding apply method in its companion object. This macro, among everything else, generates an invocation of...

Scala macros referring to a member type

scala,scala-macros,scala-2.11,scala-reflect
I have a trait with a member type, and want to have a macro with signature containing this type: trait Foo { class Bar[A] { ... } def baz[A](x: Bar[A]): Bar[A] = macro bazImpl[A] def bazImpl[A: c.WeakTypeTag](c: blackbox.Context)(x: c.Expr[Bar[A]]) = ... } This doesn't work, since bazImpl must belong either...

Scala Macro: Create new classes with Option types

scala,scala-macros,scala-2.11,scala-macro-paradise,scala-meta
I want to write a macro given this: @MetaRest case class User( @get id : Int, @get @post @patch name : String, @get @post email : String, registeredOn : DateTime ) Generate the following piece of code: object User { case class Get(id: Int, name: String, email: String) case class...

Override method from macro

scala,scala-macros
Is it possible to add method definition from macro call? Here is an example: object Macros { def someMacro = ...} trait Foo { def foo:Int } class Bar extends Foo { someMacro() } //calls to (new Bar).foo is possible and return value defined in someMacro ...

Providing implicit value for singletons in Play Json library

json,scala,playframework,scala-macros
I have following configuration: sealed trait Status case object Edited extends Status case object NotEdited extends Status case class Tweet(content:String, status:Status) I want to use Play Json format, so I guess I have to have something like this(I don't want to do it in companion object): trait JsonImpl{ implicit val...