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
1
vote
1 answer

Scala quasiquote generating parameter default value with backticks

I need to generate: case class Foo(param: Bar = BarEnum.SomeCaseObject) But this code: val term = TermName("BarEnum.SomeCaseObject") showCode(q"""case class Foo(val param : Bar = ${term})""") outputs the parameter default surrounded by…
marcprux
  • 9,845
  • 3
  • 55
  • 72
1
vote
1 answer

How to use Scala macros to create new partial functions or transform them?

I am having trouble writing a macro that transforms a given partial function and creates a new partial function. For instance, I want to be able to decompose the given partial function into its elements - pattern binder, guard condition, and body;…
winitzki
  • 3,179
  • 24
  • 32
1
vote
1 answer

Insert string into quasiquote before it's interpolated (runtime quasiquote?)

I'm trying to generate Tree for function that accepts case class value and returns value of case class parameter at given position. This is useful for extracting values of private parameters. import reflect.runtime.currentMirror import…
mixel
  • 25,177
  • 13
  • 126
  • 165
1
vote
1 answer

compiler crash when I use macros and playframework

I wrote a macro that parses JSON into a matching case class. def parse(jsonTree: JsValue): BaseType = macro parserImpl def parserImpl(c: blackbox.Context)(jsonTree: c.Tree) = { import c.universe._ val q"$json" = jsonTree val cases =…
1
vote
1 answer

Robustness of pattern matching Trees with quasiquotes

I have a macro and part of that macro consists of replacing every call to a certain method with something else. To accomplish this I use a Transformer and try to match every Tree that enters the transform method against a quasiquote. When I write it…
Jasper-M
  • 14,966
  • 2
  • 26
  • 37
1
vote
0 answers

Scala macros. Use string in quasiquotes

I'm trying to generate statement by case class parameters. case class f(a:String, b:String){ val statement = Macroses.st[f] } object MacrosTest { def start() { val result = f("1", "2").statement } } Result must be INSERT INTO…
Ivan
  • 462
  • 3
  • 13
1
vote
1 answer

How to add comments in quasiquote

I am trying to insert class comments for quasi quotes like so q""" package somePackage { /** * This is a comment */ public class SomeClass { } } """ But its throwing an exception Exception in…
user1187135
1
vote
1 answer

How to declare packages with Quasiquotes

I am trying to use quasi quote to generate the package AST. I have a string variable that lists out the package path such that val pkg = "database.dao" When I use quasi quote q"package $pkg, it tells me that I need a RefTree instead. I tried…
user1187135
1
vote
1 answer

Scala Quasiquotes Destructuring a Type

Context: I'm working on a library for working with JMX in Scala. One of the objectives is to have a strong typed interface to Managed Beans. I guess akin to to the Spring framework JMX library. Objective: Macro to Deserialise TabularData to a case…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
1
vote
0 answers

Scala 2.10.4: when parsing this quasiquote, a exception throwed

q""" val x = ${evalA.primitiveTerm} val y = ${evalB.primitiveTerm} val res = 0 for (i <- 0 until x.length; if i < y.length) { val res = x(i).compareTo(y(i)) if (res != 0) return res …
1
vote
2 answers

Generate wildcard binding pattern with macros

Scala allows pattern matching on varargs for unapplySeq: case class A(args: String*) A("a", "b", "c") match { case A(args @ _*) => args // Seq("a", "b", "c") } I want to generate such pattern with macros. How should I do it? A natural thing to…
Vladimir Matveev
  • 120,085
  • 34
  • 287
  • 296
1
vote
1 answer

AST unquoting using quasiquotes and tree returned from reify

I was trying to use Toolbox and quasiquote together to do code generation tasks, and faced with StackOverflowError while using AST of object returned from reify(x).tree, my code is as follows: abstract class A[T] {def i: T} class B(val i: Int)…
yjshen
  • 6,583
  • 3
  • 31
  • 40
1
vote
1 answer

How to match on a tree of arrow association (`x->y`) in Scala macros?

I would like to pattern match for the tree of the x -> y operation in Scala macros. I am cross compiling against Scala 2.10.4 (with Macro Paradise) and Scala 2.11.x. I have tried the following patterns and none worked: arrowTree match { case q"$x…
vjovanov
  • 120
  • 5
1
vote
1 answer

Type mismatch in scala quasiquote of macro definition: "type mismatch; found : field.NameType required: c.universe.TermName"

I asked a longer question, but it seems it's too much code for people to sort through so I've created this question to focus on one smaller, specific problem I'm facing regarding use of macros in Scala. Consider the following code snippet: val tpe =…
jonderry
  • 23,013
  • 32
  • 104
  • 171
1
vote
1 answer

scala quasiquotes convert Tree to AppliedType

I wan to infer an implicit value of an AppliedType, here's what I've done val valueType = accessorTree.returnType val encoderType = tq"DatumEncoder[$valueType]" // returns a Tree val encoder = c.inferImplicitValue(encoderType) // require a Type But…
jilen
  • 5,633
  • 3
  • 35
  • 84