scala-quasiquotes

How does one use quasiquotes to obtain the type of a value?

风流意气都作罢 提交于 2019-12-11 14:08:59
问题 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 type of value ( Int ) in tpe and assign it to U . How does one do this? 回答1: Try import scala.reflect.runtime.universe._ def getType[A: TypeTag](a: A): Type = typeOf[A] val value: Int = 3 val tpe = getType(value) // Int Connected question: How does one obtain the type of the

Scala: Dynamically generating match clauses for case classes

人走茶凉 提交于 2019-12-11 00:44:56
问题 I want to use the power of Scala's pattern matching within a set of `condition-action' rules. These rules are not known in advance, but rather are generated at runtime according to some complex critera. The algorithmic generation mechanism can be considered as a completely separate and is not part of this question, which is concerned with how to express this via Scala reflection/quasiquotes. Concretely, I'm looking to generate case definitions (of the general form case v0@x(v1,_,v2): X => f

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

自古美人都是妖i 提交于 2019-12-08 22:00:14
问题 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 adding the new constructor as a plain-old DefDef using quasiquotes in a macro annotation's impl, like: val newCtor = q"""def this() = this("", 1)""" val newBody = body :+ newCtor q"$mods class $name[..$tparams](..$first)(...$rest) extends ..$parents {

Dependent type seems to “not work” when generated by Scala macro

眉间皱痕 提交于 2019-12-07 11:55:36
问题 Apologies for the handwavey title. I’m not entirely sure how to phrase the question succinctly, since I’ve never encountered something like this before. Background info: I have the following trait, where the type U is meant to hold a Shapeless extensible record type: trait Flattened[T] { type U <: shapeless.HList def fields: U } I’m using a blackbox macro (for reasons outside the scope of this question) to create new instances of the trait: object flatten { import scala.language.experimental

Resolving the dependency of Scala Macros and Compiler Framework in SBT

陌路散爱 提交于 2019-12-06 06:43:58
问题 I am trying to write a framework to make writing Scala compiler plugins easier, what I am doing is writing a framework on top of the Scala quasiquotes. So my project depends on macros from macro-paradise and both scala-compiler and scala-reflect libraries. I wrote an SBT build script by following the instructions mentioned here: https://github.com/scalamacros/sbt-example-paradise/blob/master/project/Build.scala And used scalaVersion 2.11.0-SNAPSHOT, 2.10.3-SNAPSHOT, 2.10.3-RC1, 2.10.2 to

Dependent type seems to “not work” when generated by Scala macro

╄→尐↘猪︶ㄣ 提交于 2019-12-05 18:27:17
Apologies for the handwavey title. I’m not entirely sure how to phrase the question succinctly, since I’ve never encountered something like this before. Background info: I have the following trait, where the type U is meant to hold a Shapeless extensible record type: trait Flattened[T] { type U <: shapeless.HList def fields: U } I’m using a blackbox macro (for reasons outside the scope of this question) to create new instances of the trait: object flatten { import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context def apply[T](struct: T): Flattened[T] = macro impl