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

Liftable for function literal

Is there a way to make a Liftable for a functional literal (with 2.11)? If I have case class Validator[T](predicate: T => Boolean) val predicate = (s: String) => s.startsWith("Hi") then I want to be able to quasiquote predicate too: q"new…
Marc Grue
  • 5,865
  • 3
  • 16
  • 23
1
vote
1 answer

How to get a type in Scala AST of underscored lambda function parameter?

Not digging in details of DSL, I could write (based on that example): def InputLine = rule { Number ~ zeroOrMore("+" ~ Number ~> ((x: Int, y: Int) => x + y)) ~ EOI } I need to call lambda function from macro. Scala AST representation of it is as…
Alexander Myltsev
  • 459
  • 1
  • 3
  • 13
1
vote
1 answer

How to pass arguments to a function of arbitrary number of parameters via macro?

I have deconstructed Function, got it vparams, and now I am able to call: case class Action(f: Function, ts: List[TypeName]) { def render(ruleName: String): Expr[Unit] = c.Expr[Unit](q""" val p = ${c.prefix} val value1 =…
Alexander Myltsev
  • 459
  • 1
  • 3
  • 13
0
votes
1 answer

How to runtime compile with reflection for a class being used within another class

My code: import scala.reflect.runtime import scala.reflect.runtime.universe import scala.reflect.runtime.universe._ import scala.tools.reflect.ToolBox object Stuff { val rm: universe.Mirror = runtime.currentMirror val tb: ToolBox[universe.type]…
0
votes
2 answers

Scala 3 Macros: How to invoke a method obtained as a `Symbol` in a quoted code block?

In a Scala 3 macro that takes a type parameter T, you can use TypeRepr.of[T] and the new Scala 3 reflection API to explore the companionClass of T, and find the Symbol for an arbitrary method on that companion class (eg…
Roberto Tyley
  • 24,513
  • 11
  • 72
  • 101
0
votes
1 answer

Scala 3 macros: create a new polynmorphic function using the reflect api

I intend to create the simple polymorphic function expression below using the Quotes.reflect API: new PolyFunction { def apply[X](a: X): X = a } What I have attempted is shown below with parts that I could not implement replaced by ???: val name:…
shayan
  • 1,211
  • 9
  • 12
0
votes
1 answer

How do I ignore unused warnings in quasiquotes?

I have a macro that is using quasiquotes like: accessor.tree match { // FIXME: Get rid of the unused warning here. case q"($source) => $rhs" => validate(rhs, hasSelect = false) case t =>…
steinybot
  • 5,491
  • 6
  • 37
  • 55
0
votes
1 answer

Using ClassSymbol in a quasiquote

I have a ClassSymbol and want to generate a zero-argument method throwing ???. Here are my attempts: Assume that object Test is the type we have ClassSymbol of. I. val sym = //the ClassSymbol val tpe = tq"$sym.type" q"def foo(): $tpe =…
0
votes
1 answer

MacroAnnotation to remove annotation from method's type member

I'm learning how to write Scala Macros and wrote a macro annotation that removes an annotation from a type parameter of an annotated function. Here it is. The annotation to remove: class garbage extends StaticAnnotation Implementation of macro to…
Some Name
  • 8,555
  • 5
  • 27
  • 77
0
votes
1 answer

How to dynamically generate a type application with scala quasiquotes

I want to generate a type application so I can call a function like foo.bar[com.a.b.SomeType](baz) where com.a.b.SomeType could be any of a broad range of types. I used reflection within the macro runtime to get a reference to actual class…
Luke Eller
  • 627
  • 1
  • 7
  • 23
0
votes
2 answers

How does one obtain the type of the value that an AST represents?

I’m trying to write the following: import scala.reflect.runtime.universe._ val value: Tree = /* some AST */ val tpe = typeOf(value) // This should be the result type of the AST. // This is pseudocode. What should …
Dan Li
  • 866
  • 1
  • 7
  • 19
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

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

Use scala macros to copy method from class to companion object

I'll get straight to the business. Let's say that I have the following trait definition: trait Routable{ def routing(): String } And I'm defining the following class: case class MyEvent(name: String, age: Int) extends Routable{ override…
user1396033
  • 215
  • 3
  • 11
0
votes
1 answer

What are "stats" in Scala ASTs?

In docs.scala-lang.org/overviews/quasiquotes/syntax-summary.html#definitions, I see this "variable" called $stats all over the place. What does it stand for? Is it supposed to mean "statements"? That would be slightly odd to me since you can have…
allidoiswin
  • 2,543
  • 1
  • 20
  • 23