scala-macros

What does the Aux pattern accomplish in Scala?

旧城冷巷雨未停 提交于 2019-12-01 18:18:10
I have a bit of a sense of the Aux pattern (as used in shapeless and elsewhere) in which a type member is extracted into a type parameter, and I know it's a workaround the fact that arguments in the same argument list can't depend on each other -- but I'm not clear generally what it's used for and what problems it solves. For example, I'm currently trying to figure out how to preserve and work with the more specific type returned by a whitebox macro -- is this a usecase for Aux? Is there a simple description? Simply speaking this pattern lets you establish a relation between two generic type

What does the Aux pattern accomplish in Scala?

江枫思渺然 提交于 2019-12-01 17:12:51
问题 I have a bit of a sense of the Aux pattern (as used in shapeless and elsewhere) in which a type member is extracted into a type parameter, and I know it's a workaround the fact that arguments in the same argument list can't depend on each other -- but I'm not clear generally what it's used for and what problems it solves. For example, I'm currently trying to figure out how to preserve and work with the more specific type returned by a whitebox macro -- is this a usecase for Aux? Is there a

How to generate case objects for every field in a Scala case class using macro?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 23:08:09
I'm trying to generate case object s for every case member of every child case class of a sealed trait. I'm able to generate the code in the macro but I don't know how to use this in my code. Example usecase: sealed trait Item sealed trait Field { val name: String } case class Product(id: String, name: String) extends Item It should generate the following case object s which are fields of Product . case object ProductIdField extends Field { val name = "Product Id" } case object ProductNameField extends Field { val name = "Product Name" } Macro so far which generates the code import scala

How to generate case objects for every field in a Scala case class using macro?

十年热恋 提交于 2019-11-30 17:43:32
问题 I'm trying to generate case object s for every case member of every child case class of a sealed trait. I'm able to generate the code in the macro but I don't know how to use this in my code. Example usecase: sealed trait Item sealed trait Field { val name: String } case class Product(id: String, name: String) extends Item It should generate the following case object s which are fields of Product . case object ProductIdField extends Field { val name = "Product Id" } case object

Gradle Scala Plugin corollary to addCompilerPlugin in sbt

岁酱吖の 提交于 2019-11-30 09:34:41
What is the best way to add a Scala compiler plugin to the scalaCompile task in Gradle? Add a configuration for compiler plugins: configurations { scalaCompilerPlugin } Add compiler plugin dependencies: dependencies { scalaCompilerPlugin "org.scalamacros:paradise_2.11.7:2.1.0" } Set up the option: tasks.withType(ScalaCompile) { scalaCompileOptions.additionalParameters = [ "-Xplugin:" + configurations.scalaCompilerPlugin.asPath ] } I was able to use Macro Paradise in a Gradle-built project with this setup. 来源: https://stackoverflow.com/questions/35439912/gradle-scala-plugin-corollary-to

Documenting Scala 2.10 macros [closed]

落爺英雄遲暮 提交于 2019-11-29 20:07:00
I'll start with an example. Here's an equivalent of List.fill for tuples as a macro in Scala 2.10: import scala.language.experimental.macros import scala.reflect.macros.Context object TupleExample { def fill[A](arity: Int)(a: A): Product = macro fill_impl[A] def fill_impl[A](c: Context)(arity: c.Expr[Int])(a: c.Expr[A]) = { import c.universe._ arity.tree match { case Literal(Constant(n: Int)) if n < 23 => c.Expr( Apply( Select(Ident("Tuple" + n.toString), "apply"), List.fill(n)(a.tree) ) ) case _ => c.abort( c.enclosingPosition, "Desired arity must be a compile-time constant less than 23!" ) }

Gradle Scala Plugin corollary to addCompilerPlugin in sbt

≯℡__Kan透↙ 提交于 2019-11-29 14:53:27
问题 What is the best way to add a Scala compiler plugin to the scalaCompile task in Gradle? 回答1: Add a configuration for compiler plugins: configurations { scalaCompilerPlugin } Add compiler plugin dependencies: dependencies { scalaCompilerPlugin "org.scalamacros:paradise_2.11.7:2.1.0" } Set up the option: tasks.withType(ScalaCompile) { scalaCompileOptions.additionalParameters = [ "-Xplugin:" + configurations.scalaCompilerPlugin.asPath ] } I was able to use Macro Paradise in a Gradle-built

How to use scala macros to create a function object (to create a Map[String, (T) => T])

。_饼干妹妹 提交于 2019-11-29 11:45:33
I am trying to use Scala macros to create a case class map of single-parameter copy methods, with each method accepting a Play Json JsValue and a case class instance, and returning an updated copy of the instance. However, I am running into problems with the macro syntax for returning a function object. Given a case class case class Clazz(id: Int, str: String, strOpt: Option[String]) the intention is to create a map of the class's copy methods implicit def jsonToInt(json: JsValue) = json.as[Int] implicit def jsonToStr(json: JsValue) = json.as[String] implicit def jsonToStrOpt(json: JsValue) =

How can I create an instance of a Case Class with constructor arguments with no Parameters in Scala?

♀尐吖头ヾ 提交于 2019-11-29 11:08:05
I'm making a Scala app that sets by reflection field values. This works OK. However, in order to set field values I need a created instance. If I have a class with an empty constructor, I can do this easily with classOf[Person].getConstructors.... However, when I try doing this with a Case class with a non empty constructor It doesn't work. I have all of the field names and its values, as well as the Object type I need to create. Can I instance the Case Class somehow with what I've got? The only thing I don't have is the parameter names from the Case Class constructor or a way to create this

Is there a way to test at compile-time that a constant is a compile-time constant?

情到浓时终转凉″ 提交于 2019-11-29 09:57:48
Given how difficult it is to know whether an arithmetic final val expression will be compiled to a compile-time constant, and how easy it is to accidentally break compile-time-ness ... Can anyone think of an easy way to verify, at compile-time, that the compiler has actually created a compile-time constant from, say, a complex arithmetic expression? I'm guessing this might be some kind of annotation or macro, but maybe there's something simpler. For example, maybe something like: @CompileTime final val HALF_INFINITY = Int.MaxValue / 2 would be possible. Luckily enough, macros are wired into