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
1 answer
scala: How to obtain class name through complex polymorphism with compile time macros?
When attempting to get the name of a class via a WeakTypeTag reference when defining a macro implementation, I can't seem to get the proper info if multiple layers of polymorphism are applied.
For example if I have the following setup:
object…

user675446
- 85
- 5
0
votes
1 answer
How does one use quasiquotes to obtain the type of a value?
I’m trying to write the following:
val value: Int = 3
val tpe = typeOf(value) // This is pseudocode. What should
// actually go on this line?
q"""
type U = $tpe
val v: U = $value
"""
Essentially, I need to capture the…

Dan Li
- 866
- 1
- 7
- 19
0
votes
1 answer
Class vs Type annotations with Shapeless
As per the scala documentation, there are four kinds of annotations:
Class annotations: @ClassAnnotation case class Foo(...)
Variable/Value annotations: @ValAnnotation val field: String
Type annotations: field: String @TypeAnnotation
Expression…

pgrandjean
- 676
- 1
- 9
- 19
0
votes
1 answer
In Scala.js, how would you traverse object graphs navigationally?
Suppose that you want to traverse an object graph in a navigational way, similar to the way we traverse file systems.
For example, imagine you have an object graph that supports this expression:
var x = objName.foo.bar.baz.fieldName
We can encode…

Ben McKenneby
- 481
- 4
- 15
0
votes
1 answer
How to get a `Symbol` of a member that is available via an implicit conversion?
I'm writing a macro which generate some code like this:
q"_root_.ru.lmars.macropack.TagsAndTags2.$tagName(..$tagParams)"
but I want to generate this code only if $tagName is defined and have some "marker" (like annotation or some special return…

lmars
- 302
- 1
- 6
0
votes
1 answer
macro has not been expanded - circe macro - solution found already
At this line I get a compiler error :
macro has not been expanded.
Here is the code snippet :
package app.client.rest.commands.generalCRUD
import app.shared.data.model.Entity.{Data, Entity}
import app.shared.data.model.{DataType, LineText,…

jhegedus
- 20,244
- 16
- 99
- 167
0
votes
1 answer
print string whenever variable get changed with scala macro
If I have defined a variable b val b:B. Is it possible to print the value of b each time b is assigned to a new value. Like the code below:
case class B(name:String) {
}
var b = B("123")
b = B("xxx")
println(s"b is changed to $b")
b =…

worldterminator
- 2,968
- 6
- 33
- 52
0
votes
1 answer
How can I consume a Scala macro/quasiquote for code templates?
I want to generate a bunch of objects at compile time that follow a simple pattern, so I wrote the following macro:
object MyMacro {
def readWrite[T](taName: String, readParse: String => T, label: String, format: T => String): Any = macro…

Greg
- 10,696
- 22
- 68
- 98
0
votes
3 answers
Parsing JSON based off of schema with recursive fields in Scala
I have a json-schema (https://json-schema.org) with recursive fields, and I would like to programmatically parse json that adheres to the schema in Scala.
One option is to use Argus (https://github.com/aishfenton/Argus), but the only issue is that…

Basil Ahmad
- 3
- 1
0
votes
1 answer
Generic Macros with Quill
Hi so I've been trying to create some generic functions using macros and Quill.
Here is my implementation of the macro:
class Impl(val c: Context) {
import c.universe._
def all[T](tblName: Tree, ctx: Tree)(implicit t: WeakTypeTag[T]): Tree =
…

Vangogh500
- 939
- 1
- 7
- 17
0
votes
1 answer
runtime logger exception with dotty
My project has a dependency on scala logging library and I am trying to upgrade my project to use dotty.
For that I cloned sample dotty project from https://github.com/lampepfl/dotty-example-project and updated it include logging libraries. Please…

mogli
- 1,549
- 4
- 29
- 57
0
votes
2 answers
Scala macros example not working on Scala 2.12.6
I am taking the following code from here.
package example.macros
import scala.language.experimental.macros
import scala.reflect.macros.Context
import scala.collection.mutable.{ListBuffer, Stack}
object Macros {
def printf(format: String, params:…

Michael Lafayette
- 2,972
- 3
- 20
- 54
0
votes
0 answers
Avro4s generic toByteArray / fromByteArray
I has a problems with generic version toByteArray / fromByteArray functions for Avro4s
I found this Avro serialization with generic type issue
this is works:
def fromByteArray[A: SchemaFor : FromRecord](bytes: Array[Byte]): Option[A] = {
val…

HoTicE
- 573
- 2
- 13
0
votes
1 answer
Scala macro: generate a class method based on Config
Is there a way to generate a method within a particular class/trait/object based on TypesafeConfig object at compile time?
For instance, I have this:
object Main {
val config: Config = ConfigFactory.parseString(
"""
|object {
| …

Pavel
- 11
- 2
0
votes
1 answer
Macros or Scala Reflect - extract all parameter names as a list of string routes
Given I have a case class with a lot of different parameter types, I want to collect all parameter names in a dot notation.
Example:
case class Language(name: String, locale: String)
case class Author(name: String)
case class ContentType(name:…

raul782
- 479
- 1
- 5
- 15