Scala macros is a facility for Scala—a general-purpose programming language—that permits compile-time metaprogramming against Scala syntax trees, from within the Scala language.
Questions tagged [scala-macros]
774 questions
13
votes
1 answer
Why does the runtime reflective universe and the macro universe create two different trees for scala.None?
If I have a macro which tranforms code such as:
(src: a.b.c.TestEntity) =>
{
z.y.TestTable(None)
}
To match the None part of that AST I can use an extractor such as:
object NoneExtractor {
def unapply(t: Tree): Boolean = t…

simbo1905
- 6,321
- 5
- 58
- 86
13
votes
2 answers
Enabling the macro-paradise Scala compiler plugin in Maven projects
I have the ordinary scala-2.10 macros working in a maven project just by including the scala-reflect.jar library as a dependency in the pom, but what do I need to turn on macro-paradise? I am using scala-2.10 and scala-maven-plugin-3.1.5.

Daniel Mahler
- 7,653
- 5
- 51
- 90
13
votes
2 answers
How to use Type calculated in Scala Macro in a reify clause?
I've been working with Scala Macros and have the following code in the macro:
val fieldMemberType = fieldMember.typeSignatureIn(objectType) match {
case NullaryMethodType(tpe) => tpe
case _ =>…

mgonto
- 6,605
- 2
- 29
- 36
12
votes
1 answer
Extracting and accessing fields at compile time in Scala 3
Extracting names and types of elements of a case class at compile time in Scala 3 has been already explained well in this blog: https://blog.philipp-martini.de/blog/magic-mirror-scala3/
However, the same blog uses productElement to get the values…

Koosha
- 1,492
- 7
- 19
12
votes
2 answers
Getting Parameters from Scala Macro Annotation
So I have an annotation on a function (DefDef). This annotation has parameters.
However, I am confused on how to get the parameters from the constructor.
Usage example:
class TestMacro {
@Foo(true)
def foo(): String = ""
foo
}
Here's the…

ilinum
- 464
- 4
- 11
12
votes
1 answer
scala 2.10.2 calling a 'macro method' with generic type not work
I define following macro to transform case fields to map
import scala.language.experimental.macros
import scala.reflect.macros.Context
def asMap_impl[T: c.WeakTypeTag](c: Context)(t: c.Expr[T]) = {
import c.universe._
val…

jilen
- 5,633
- 3
- 35
- 84
12
votes
1 answer
Method cannot be accessed in Macro generated class
I have the following macro defining a class and returning an instance of that class (with Scala 2.10.2 and the macro plugin):
def test[T] = macro testImpl[T]
def testImpl[T : c.WeakTypeTag](c: Context): c.Expr[Any] = {
import c.universe._
val…

Eric
- 15,494
- 38
- 61
12
votes
2 answers
Introspect argument passed to a Scala macro
I would like to program a Scala macro that takes an instance of a case class as argument. All objects that can be passed to the macro have to implement a specific marker trait.
The following snippet shows the marker trait and two example case…

MontChanais
- 123
- 6
11
votes
2 answers
create an ambiguous low priority implicit
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 =…

jwvh
- 50,871
- 7
- 38
- 64
11
votes
1 answer
How can parameters/settings be passed to a Scala macro?
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)…

Emiswelt
- 3,909
- 1
- 38
- 56
11
votes
2 answers
How to test Scala macros?
What is the proposed way of performing tests on scala macros?
I realize that one needs two projects due to the necessity of separate compilation. This step, if necessary, is acceptable and mostly clear.
But how do you assert a macro expansion fails…

Martin Ring
- 5,404
- 24
- 47
11
votes
3 answers
Is there a way to test at compile-time that a constant is a compile-time constant?
Given how difficult it is to know whether an arithmetic final val expression will be compiled to a compile-time constant, and how easy it is to accidentally break compile-time-ness...
Can anyone think of an easy way to verify, at compile-time, that…

Ed Staub
- 15,480
- 3
- 61
- 91
11
votes
3 answers
Custom Scala enum, most elegant version searched
For a project of mine I have implemented a Enum based upon
trait Enum[A] {
trait Value { self: A =>
_values :+= this
}
private var _values = List.empty[A]
def values = _values
}
sealed trait Currency extends Currency.Value
object…

vchuravy
- 1,208
- 10
- 22
11
votes
1 answer
Scala Macros: Checking for a certain annotation
Thanks to the answers to my previous question, I was able to create a function macro such that it returns a Map that maps each field name to its value of a class, e.g.
...
trait Model
case class User (name: String, age: Int, posts: List[String])…

Emre
- 1,023
- 2
- 9
- 24
10
votes
1 answer
How to access parameter list of case class in a dotty macro
I am trying to learn meta-programming in dotty. Specifically compile time code generation. I thought learning by building something would be a good approach. So I decided to make a CSV parser which will parse lines into case classes. I want to use…

Bilal Fazlani
- 6,727
- 9
- 44
- 90