Questions tagged [scala-quasiquotes]

In Scala, quasiquotes are shipped in the official Scala distribution as part of scala-reflect.jar

Read also: http://docs.scala-lang.org/overviews/quasiquotes/intro.html

91 questions
2
votes
1 answer

How to use quasiquotes with previously defined object

I just started studying scala compile-time reflection, and I got introduced to quasiquotes by the Scala official guides. One concept I'm still struggling with is how am I supposed to work with quasiquotes (or reify, for that matter) if I want to…
2
votes
1 answer

How can I use generics for a Scala (2.12) macro?

I've defined a simple generic macro: object MyMacro { def readWrite[T](readParse: String => T, label: String, format: T => String): Unit = macro readWriteImpl[T] def readWriteImpl[T: c.WeakTypeTag](c: Context)(readParse: c.Expr[String => T],…
Greg
  • 10,696
  • 22
  • 68
  • 98
2
votes
1 answer

Evalutate complex type with quasiquote scala, unlifting

I need to compile function and then evaluate it with different parameters of type List[Map[String, AnyRef]]. I have the following code that does not compile with such the type but compiles with simple type like List[Int]. I found that there are…
Oleg
  • 575
  • 4
  • 13
2
votes
1 answer

Scala macro annotation - case class with type parameters

I'm trying to write a simple macro annotation for case classes which adds a method to the companion object. The catch is that the new method must account for type parameters on the annotated case class. Here's the test that I need to pass package…
2
votes
1 answer

Use Scala Quasiquotes to generate a class which inherits from a known class

This is easier to explain in code. I want to do something like: import scala.reflect.runtime.currentMirror import scala.tools.reflect.ToolBox val toolbox = currentMirror.mkToolBox() val universe: scala.reflect.runtime.universe.type =…
devth
  • 2,738
  • 4
  • 31
  • 51
2
votes
0 answers

How do I extract primary constructor params with a quasiquote

I'm currently using: annottees map (_.tree) match { case (classDef @ q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }") :: Nil => to extract both the classDef and the…
2
votes
1 answer

Unquote applied types in scala macros

In the scala console I can do the following without a problem : scala> val tree = q"def f():MySuperType[(Char,Char)]" tree: universe.DefDef = def f(): MySuperType[scala.Tuple2[Char, Char]] scala> val q"def $f():$d" = tree f: universe.TermName =…
tir0nik
  • 31
  • 3
2
votes
1 answer

How can I splice in a type and a default value in Scala quasiquotes?

I'm trying to make a type-provider that gives updated case classes. How might I splice in a type and a default value (or omit the default value)? def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { import c.universe._ import…
Julian Peeters
  • 853
  • 1
  • 6
  • 19
2
votes
1 answer

How do I splice symbols of various types in a quasiquote?

I am developing a macro and in its implementation I get the weakTypeOf T where T is a type parameter of the macro function. I want to splice info, from the method definitions of this concrete type to a new class declaration tree. I cannot get the…
Aggelos Biboudis
  • 1,175
  • 8
  • 27
2
votes
1 answer

Unapply/pattern matching with scala macros and quasiquotes for knownDirectSubclasses

I'm trying to create a match statement using macros, that matches all subclasses of a given type. But I have trouble with extracting the field values of the case classes. For example: sealed abstract class Foobar case class Foo(x:Int,f:Foobar)…
schlicht
  • 4,735
  • 1
  • 13
  • 23
1
vote
3 answers

Quasiquotes in Scalafix

Here is Spark 2.4 code using unionAll import org.apache.spark.sql.{DataFrame, Dataset} object UnionRewrite { def inSource( df1: DataFrame, df2: DataFrame, df3: DataFrame, ds1: Dataset[String], ds2: Dataset[String] ): Unit =…
mvasyliv
  • 1,214
  • 6
  • 10
1
vote
1 answer

`tq` equivalent in Scala 3 macros

With Scala2 I could implement a macro and generate types using tq quasiquote syntax, for eg: q""" new Foo { type Bar = ${tq"(..$params)"} } """ I am able to do two things with this syntax - Able to define a type Bar based on the…
tusharmath
  • 10,622
  • 12
  • 56
  • 83
1
vote
0 answers

Scala macro: get companion object from class type

I cannot manage to get the companion object / singleton from a class type in Scala macro / quasiquotes. Tried to follow https://docs.scala-lang.org/overviews/quasiquotes/type-details.html#singleton-type, the given example works but it is based on a…
1
vote
0 answers

Using existing methods in macro

Suppose i have some class with some methods class Clz ... { def someMethod: Map[String, Long] = ... def id: Long = 0L } i'm need to reuse someMethod and overwrite id i don't know why but it's throw Stackoverflow and also i'm need to do…
1
vote
1 answer

merge `Tree` and `List[Tree]` in scala compile-time reflection

I have scala macro that creates the constructor for a class on the fly. So for example if we have a class case class PersonConfig(name: String, age: Int, isFemale: Boolean). I have the Tree structure for the classname and the arguments passed to the…
rogue-one
  • 11,259
  • 7
  • 53
  • 75