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
0
votes
3 answers
How to avoid definition of implicit reads/writes for the sub-classes for JSON Macro Inception (to convert nested JSON structure to Scala)
I have a requirement where the incoming JSON object is complex and mostly nested ex:
"users": {
"utype": "PERSON",
"language":"en_FR",
"credentials": [
{
"handle": "xyz@abc.com",
"password": "123456",
…

Anand
- 601
- 2
- 7
- 17
0
votes
0 answers
names of traits to be mixed are given as strings in a List
I have something like this:
trait Color {
def myname: String = ""
}
trait White extends Color {
override def myname = super.myname + " white "
}
trait Green extends Color {
override def myname = super.myname + " green "
}
trait Yellow…

Valerin
- 465
- 3
- 19
0
votes
2 answers
Generate a case class in scala
How to generate case classes in scala based on information kept in 100s of java classes? Basically, I need this for writing a wrapper for a java library.
Is it even possible? I have a feeling that no. In that case what is the best alternative?

Anton Kuzmin
- 821
- 1
- 10
- 26
0
votes
1 answer
ambiguous implicit values when using contravariant generic type
I've run into a problem with inferImplicitValue in a scala macro. I was playing around with a macro for play's json libary Format[T].
I could narrow it down to a problem how Writes[T] is sometimes implemented with OWrites[T]. Together with an…

Jaap
- 3,081
- 2
- 29
- 50
0
votes
1 answer
WeakTypeTag not working in implicit macro with no value argument
I'm using an implicit macro to generate a typeclass.
trait ColumnType[+A]
object ColumnType {
implicit def materializeColumnType[A <: Product]: ColumnType[A] = macro MappedColumnTypeMacro.materializeColumnType[A]
}
case class…

Brendanator
- 833
- 1
- 8
- 15
0
votes
1 answer
Scala macro and type erasure
I'm having some problems with a macro I've written to help me log metrics represented as case class instances to to InfluxDB. I presume I'm having a type erasure problem and that the tyep parameter T is getting lost, but I'm not entirely sure what's…

Isvara
- 3,403
- 1
- 28
- 42
0
votes
1 answer
Exception during macro expansion with sbt-native-packager and ScalaFXML
I have a small project on github that I created to explore ScalaFx and a few other things I want to use on a bigger project. For now, it includes a single sub-project, which is a simple calculator written using ScalaFX and ScalaFXML.
The project…

Cyäegha
- 4,191
- 2
- 20
- 36
0
votes
1 answer
Dealias in class scope
I wrote a simple annotation macro to expand a type synonym and save the result in a string. It works fine in a block, but does not work when used to declare a class member. Why?
Here's all the code necessary to compile and run.
Client code:
object…
0
votes
1 answer
How do I get the type from an Ident() in a macro?
I'm trying to write a generic macro in which I can grab any trees that match foo.bar, but only where foo is a Foo. I only need to look inside the enclosing class where the macro is called. So far I have this:
def getFooTrees[T](): Unit = macro…

colinmarc
- 2,421
- 1
- 22
- 34
0
votes
1 answer
Reify a ValDef from compile to runtime
I want to reify a ValDef into runtime, but i does not work directly. If i encapsulate the ValDef into a Block, everything works perfectly, like in the following example:
case class Container(expr: Expr[Any])
def lift(expr: Any): Container = macro…

akunft
- 3
- 1
0
votes
0 answers
What is a signature for implicit generic function, that uses Scala macros for generation
I need to serialise in JSON - sealed trait hierarchy, which looks something like:
sealed trait Foo[T]
case class Bar(x: Int) extends Foo[Int]
case class Baz(s: String) extends Foo[String]
case object Bah extends Foo[Void]
I'm using…

mavarazy
- 7,562
- 1
- 34
- 60
0
votes
1 answer
How does the case by type work in scala?
I know about case classes, pattern matching, unapply and PartialFunction, but I'm confused about bellow macros snippet.
val declarations = weakTypeOf[T].declarations
val methods = declarations.collect { case m: MethodSymbol => m }
Scaladoc of…

sh1ng
- 2,808
- 4
- 24
- 38
0
votes
1 answer
Can I use Java reflection to get the value of a member that has been added with a Scala macro annotation?
I have a Scala macro annotation that adds a val "x" to a class MyRecord at compile time.
I've also got some Java code that uses MyRecord.class.getDeclaredField(x) to get the value of the added field.
javap MyRecord.class shows that the added field…

Julian Peeters
- 853
- 1
- 6
- 19
0
votes
1 answer
Is it possible to achieve functionality provided by implicit classes via macros?
We are pretty familiar with implicits in Scala for now, but macros are pretty undiscovered area (at least for me) and, despite the presence of some great articles by Eugene Burmako, it is still not an easy material to just dive in.
In this…

tkroman
- 4,811
- 1
- 26
- 46
0
votes
1 answer
Is it possible to have a scala macro constrain the type of a method?
Basically I'm imagining the following.
trait A[T] {
def get(idx: Int): U
}
The macro comes in the use. Ideally I want to be able to use get(0), get(1) as macros where the proper return type will be known. Is this possible?

A Question Asker
- 3,339
- 7
- 31
- 39