scala-macros

How to distinguish compiler-inferred implicit conversion from explicitly invoked one?

99封情书 提交于 2019-12-10 03:12:27
问题 Let's imagine passing these two equivalent expressions to a Scala macro: with compiler-inferred implicit conversion: 1+"foo" with explicitly invoked implicit conversion: any2stringadd(1)+"foo" Is there a way to distinguish between these two inside the macro? 回答1: First of all, the 1 + "foo" case is going to be tricky because there isn't actually any implicit conversion happening there: Int itself really, truly does have this + method (unfortunately). So you're out of luck if that's your use

Is it possible to write a scala macro whose returntype depends on argument?

无人久伴 提交于 2019-12-09 23:22:14
问题 For a DSL I would like to be able to do something like: object Creator { def create[T](s :String) :Foo[T] = macro createImpl[T] def createImpl[T](c :Context)(s :c.Expr[String]) : c.Expr[Foo[T]] = { reify(new Foo[Any]()) } } My problem is to replace the Any in reify with something that will return the correctly parametrized version. (above I use a string argument, but in the final version I plan to use the companion object of class T as a marker to know the argument-type of a Function1[T,Unit]

Scala Macros: Checking for a certain annotation

风流意气都作罢 提交于 2019-12-09 14:58:02
问题 Thanks to the answers to my previous question, I was able to create a function macro such that it returns a Map that maps each field name to its value of a class, e.g. ... trait Model case class User (name: String, age: Int, posts: List[String]) extends Model { val numPosts: Int = posts.length ... def foo = "bar" ... } So this command val myUser = User("Foo", 25, List("Lorem", "Ipsum")) myUser.asMap returns Map("name" -> "Foo", "age" -> 25, "posts" -> List("Lorem", "Ipsum"), "numPosts" -> 2)

Using attachments with macros in Scala 2.10

一世执手 提交于 2019-12-09 12:22:17
问题 Update: I suspect that what I want might not be possible, and I've written up a blog post with my reasoning (and some alternatives) here. I'd be very happy to be told that I'm wrong. Suppose I want to create a instance of a trait using a factory method with a macro implementation. This method will take as an argument a path to a resource that the macro will read and parse (at compile time) into a map from strings to strings. That part is all fairly straightforward. Now suppose I want to

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 {

troubleshooting jar loading conflicts in sbt

家住魔仙堡 提交于 2019-12-08 16:35:21
问题 I get the following error on sbt startup, when two specific sbt plugins are added together to a project in its build definition. One of these sbt plugins is scalikejdbc and the other is my own, and clearly their mutual inclusion in a project's build definition results in this error upon sbt startup: scala.reflect.internal.Types$TypeError: package macros contains object and package with same name: blackbox Clearly, it looks as if each plugin brings along a different version of scala.reflect

How to colorize the standard output in scala with ansi codes

陌路散爱 提交于 2019-12-08 10:24:38
问题 Is there a scala library that can help colorize text written to the standard output? This library should take advantage of the scala string interpolation mechanic. 回答1: If you're using scala 2.11+ you can use this library : https://github.com/backuity/ansi-interpolator It uses a macro to transform your strings at compile time, and supports nesting: ansi"Text containing ansi tags such as %bold{bold text} or %underline{can be %yellow{nested}}" // you can also use string interpolation: val

Adding extra trait to object using scala macro annotation

孤人 提交于 2019-12-08 07:10:34
问题 I'm on Scala 2.10.3 using Macro Paradise. I have a macro annotation where I'm trying to add a trait to on object, e.g: @MyAnnotation object Foo extends Bar {} After expansion I want something like: object Foo extends Bar with Baz {} Where Baz is a trait accessible in the compilation scope. Using macro paradise I can cleanly destructure my target tree: q"object $obj extends ..$bases { ..$body }" = tree where bases holds the existing extensions in the form List of Ident(newTypeName("Bar")) I

Splicing together symbols with Scala macros

喜你入骨 提交于 2019-12-08 00:19:41
问题 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

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

亡梦爱人 提交于 2019-12-07 14:47:38
问题 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