scala-macros

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

Way to enhance a class with function delegation

冷暖自知 提交于 2019-12-07 04:03:49
问题 I have the following classes in Scala: class A { def doSomething() = ??? def doOtherThing() = ??? } class B { val a: A // need to enhance the class with both two functions doSomething() and doOtherThing() that delegates to A // def doSomething() = a.toDomething() // def doOtherThing() = a.doOtherThing() } I need a way to enhance at compile time class B with the same function signatures as A that simply delegate to A when invoked on B. Is there a nice way to do this in Scala? Thank you. 回答1:

Provide implicits for all subtypes of sealed type

自闭症网瘾萝莉.ら 提交于 2019-12-06 14:58:58
In my application I have multiple case classes and objects which are part of sealed trait hierarchy. I use them as messages in Akka. Those classes need to be converted to user friendly form before sending through websocket. Previously I used big pattern match to convert them in single place, but as number of types grows I would like to use implicit conversion: object Types { sealed trait Type case object SubType1 extends Type case object SubType2 extends Type case object SubType3 extends Type trait Converter[T] { def convert(t: T): Int } } object Implicits { import Types._ implicit object

Splicing together symbols with Scala macros

血红的双手。 提交于 2019-12-06 12:10:20
I am trying to call a specialized collections library like FastUtil or Trove from generic Scala code. I would like to implement something like def openHashMap[@specialized K, @specialized V]: ${K}2${V}OpenHashMap = new ${K}2${V}OpenHashMap() Where the ${X} is clearly not valid Scala, but just my meta notation for text substitution, so that openHashMap[Long, Double] would return a Long2DoubleOpenHashMap the type would be known at compile time. Is this possible with Scala macros. If so, which flavour? I know there are def macros, implicit macros, fundep materialization, macro annotations, type

Macro dependancy appearing in POM/JAR

喜欢而已 提交于 2019-12-06 10:24:48
I have a scala project that uses macros which basically follows the exact method described here ( http://www.scala-sbt.org/0.12.4/docs/Detailed-Topics/Macro-Projects.html ) including the whole Distribution section (so in essence I have a root project, and a subproject called macro which holds the macros being used) The problem is, when I publish my project (using publish-local for now), and another scala project uses the one with a macro as a dependency, it tries to pull macro#macro_2.10;0.1-SNAPSHOT since it appears in the POM. This causes project to fail to compile as it can't resolve the

How to replace a subtree with some other tree?

久未见 提交于 2019-12-06 08:52:48
问题 In Scala macro, I want to do something like this: I have a Tree (possibly large). Now I want to find a subtree of this tree that has some concrete form, e.g. Apply(_, _) . And now I want to create a new tree that is a copy of the original tree, but the found subtree is replaced by some other tree. With something like this, I could for example replace invocation of some method with invocation of some other method. Is something like this possible? 回答1: I'm very interested in seeing alternative

Scala Macro: get param default value

僤鯓⒐⒋嵵緔 提交于 2019-12-06 06:58:18
问题 I have the next code, and i would like to extract the default parametr from value. // def extractor[T] = macro extractorImpl[T] def extractorImpl[T: c.WeakTypeTag](c: Context) = { //first i got a type contructor ??? } i try with attachments but attachments.all return a Set[Any] with (for example) SymbolSourceAttachment(val name: String = "new name") SymbolSourceAttachment contain ValDef but i do not know how to extract from SymbolSourceAttachment ValDef . By the way i should to get a Map

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

How to print source code of “IF” condition in “THEN”

跟風遠走 提交于 2019-12-06 02:53:04
I would like to print Scala source code of IF condition while being in THEN section. Example: IF{ 2 + 2 < 5 } THEN { println("I am in THEN because: " + sourceCodeOfCondition) } Let's skip THEN section right now, the question is: how to get source code of block after IF? I assume that IF should be a macro... Note: this question is redefined version of Macro to access source code of function at runtime where I described that { val i = 5; List(1, 2, 3); true }.logValueImpl works for me (according to other question Macro to access source code text at runtime ). Travis Brown Off-the-cuff

Scala Macro get value for term name

試著忘記壹切 提交于 2019-12-06 02:49:15
问题 I have a following code: usage.scala object Test extends App { import Macros._ val f = 1 Macros.get(f) } macros.scala import language.experimental.macros import scala.reflect.macros.Context object Macros { def get(a: Int) = macro getImpl def getImpl(c: Context)(a: c.Expr[Int]) = { import c.universe._ println(showRaw(a)) } } It return: Expr(Select(This(newTypeName("Test")), newTermName("f"))) How to extract from termName("f") a 1 value ? It's possible with macros? 回答1: In general - no. f could