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
274
votes
1 answer

How to use Shapeless in a Quasiquote?

I'm trying to call a Shapeless macro from inside a quasiquote with Scala and I'm not getting what I would like to get. My macro doesn't return any errors but it doesn't expand Witness(fieldName) into Witness.Lt[String] val implicits =…
Roch
  • 21,741
  • 29
  • 77
  • 120
29
votes
1 answer

Matching function literals with quasiquotes in Scala

This question is similar in motivation to my previous question (although it's about a problem I ran into in a different context). I can pattern match on a function literal pretty easily without quasiquotes: import scala.reflect.macros.Context import…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
10
votes
1 answer

Scala quasiquote concatenation

I'm new to scala macros and I spent a couple of days trying to write my very first one. I have a problem with quasiquotes concatenation. There is a list of case clauses, let's say the following: val cases = cq"x => 1 " :: cq"_ => 0 " :: Nil And I…
9
votes
2 answers

Splicing a passed function body into a macro-rewritten expression

I was playing with Scala 2.11's new macro features. I wanted to see if I could do the following rewrite: forRange(0 to 10) { i => println(i) } // into val iter = (0 to 10).iterator while (iter.hasNext) { val i = iter.next println(i) } I think…
KChaloux
  • 3,918
  • 6
  • 37
  • 52
9
votes
1 answer

Quasiquotes for multiple parameters and parameter lists

Quasiquotes are amazing—they make writing macros in Scala hugely less painful, and in my experience they almost always just work exactly as I'd expect. And best of all, they're now available as a plugin in Scala 2.10. This question is about a small…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
8
votes
1 answer

How do I add a no-arg constructor to a Scala case class with a macro annotation?

I'm trying to answer this question. Instead of writing: case class Person(name: String, age: Int) { def this() = this("",1) } I thought I'd use macro annotations to expand it from: @Annotation case class Person(name: String, age: Int) So I tried…
8
votes
2 answers

Matching XML Literals in Scala Macros

I want to transform Scala XML literals with a macro. (Not a string literal with XML but actual XML literals). As far as I understand, XML literals are not actually built into the language on the AST level but are desugared in the parser.…
Martin Ring
  • 5,404
  • 24
  • 47
7
votes
1 answer

How does the Scala compiler perform implicit conversion?

I have a custom class, A, and I have defined some operations within the class as follows: def +(that: A) = ... def -(that: A) = ... def *(that: A) = ... def +(that: Double) = ... def -(that: Double) = ... def *(that: Double) = ... In order to have…
darsnack
  • 915
  • 5
  • 10
6
votes
1 answer

How to get the runtime value of parameter passed to a Scala macro?

I have an ostensibly simple macro problem that I’ve been banging my head against for a few hours, with no luck. Perhaps someone with more experience can help. I have the following macro: import scala.language.experimental.macros import…
Dan Li
  • 866
  • 1
  • 7
  • 19
5
votes
1 answer

"macro implementation reference has wrong shape" in the Scala Documentation examples

The following macro is pasted from http://docs.scala-lang.org/overviews/quasiquotes/usecases.html: import reflect.macros.Context import language.experimental.macros val universe = reflect.runtime.universe; import universe._ import…
Ben Kovitz
  • 4,920
  • 1
  • 22
  • 50
4
votes
2 answers

Convert String expression to actual working instance expression

I am trying to convert an expression in Scala that is saved in database as String back to working code. I have tried Reflect Toolbox, Groovy, etc. But I can't seem to achieve what I require. Here's what I tried: import…
4
votes
0 answers

Recursively wrapping method invocations with compiler plugins/macros

OUTLINE I have an API that looks something like this: package com.example object ExternalApi { def create[T <: SpecialElement](elem: T): TypeConstructor[T] = TypeConstructor(elem) def create1[T <: SpecialElement](elem: T):…
4
votes
0 answers

Scala macros. How to instantiate subclass and dynamically mixin trait

Motivation: (not tightly associated with this topic but states a use case. Skippable) I'm using Scala.js to write React code. I now have a form which contains bunch of inputs, each needs a onChange callback in its props. See Controlled…
cuz
  • 1,172
  • 11
  • 12
4
votes
1 answer

Lifting string variable using Scala quasiquotes

This is a simplified version of the problem I am facing but the underlying issue remains. After calling a macro, I want to generate case classes dynamically. I am able to retrieve parameters from macro call etc. The issue I am having is trying to…
brioche
  • 43
  • 4
4
votes
1 answer

Scala Quasiquote Lifting

The Scala documentation for Quasiquotes mentions this when explaining Lifting: One can also combine lifting and unquote splicing: scala> val ints = List(1, 2, 3) scala> val f123 = q"f(..$ints)" f123: universe.Tree = f(1, 2, 3) scala> val…
1
2 3 4 5 6 7