scala-macro-paradise

SBT Plugin: How to add compiler plugin as a dependency that is not propagated downstream?

若如初见. 提交于 2019-12-10 14:48:20
问题 I'm writing a SBT plugin. I would like to use Circe JSON library, but it requires the Macro Paradise compiler plugin on Scala 2.10. Normally you add compiler plugins to build.sbt and SBT plugins to project/plugins.sbt . Now when you're building a SBT plugin, the other plugins become dependencies, so you put them to build.sbt and they are propagated to the projects where you use your SBT plugin. When I put the following snippet in build.sbt of my SBT plugin: addCompilerPlugin("org.scalamacros"

setting correct scala version on scala ide

有些话、适合烂在心里 提交于 2019-12-10 10:40:10
问题 I'm trying to work on a project on scala IDE but I've having build problems on scala IDE. On sbt the project builds fine. I used the eclipse sbt plugin and imported the project on scala IDE. There were build errors, which makes the ide close to useless. One of the errors is Compiler plugin paradise_2.12.1-2.1.0.jar is cross-compiled with incompatible version for this project: 2.12.1 vs 2.12.2 I thought scala minor versions were compatible, though I see there is an exception for some

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

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

Use Scala macros to generate methods

送分小仙女□ 提交于 2019-12-03 13:00:20
问题 I want to generate aliases of methods using annotation macros in Scala 2.11+. I am not even sure that is even possible. If yes, how? Example - Given this below, I want the annotation macros to expand into class Socket { @alias(aliases = Seq("!", "ask", "read")) def load(n: Int): Seq[Byte] = {/* impl */} } I want the above to generate the synonym method stubs as follows: class Socket { def load(n: Int): Seq[Byte] = // .... def !(n: Int) = load(n) def ask(n: Int) = load(n) def read(n: Int) =

Scala macro to print code?

大兔子大兔子 提交于 2019-11-29 07:32:31
I want to do something like this: def assuming[A](condition: => Boolean)(f: => A): A = { require(condition, /* print source-code of condition */) f } Sample usage: def fib(n: Int) = n match { // yes, yes, I know this is not efficient case 0 => 0 case 1 => 1 case i => assuming(i > 0) { fib(i-1) + fib(i-2) } } Now, for example, if you call fib(-20) , I want it to throw an exception with a message like Assertion failed: -20 > 0 or Assertation failed: i > 0 Dude, isn't an assert macro one of the basic use cases you implement to learn how to use macros? Well, that's what I thought, too. By "glean