scalameta

Scala conditional compilation

自古美人都是妖i 提交于 2021-01-20 12:23:06
问题 I'm writing a Scala program and I want it to work with two version of a big library. This big library's version 2 changes the API very slightly (only one class constructor signature has an extra parameter). // Lib v1 class APIClass(a: String, b:Integer){ ... } // Lib v2 class APIClass(a: String, b: Integer, c: String){ ... } // And my code extends APIClass.. And I have no #IFDEF class MyClass() extends APIClass("x", 1){ // <-- would be APIClass("x", 1, "y") in library v2 ... } I really don't

Macro annotation to override toString of Scala function

假如想象 提交于 2020-07-04 01:54:33
问题 How to write macro annotation which looks in usage like @named("+2") _ + 2 and produces: new (Int => Int) { override def toString(): String = "+2" def apply(x: Int): Int = x + 2 } 回答1: Correct syntax is ((_: Int) + 2): @named("+2") . Unfortunately macro annotations annotating expressions don't expand. The simplest is to use object Named { def build[T, R](name: String)(applyFunc: T => R): T => R = new (T => R) { override def toString() = name def apply(x: T): R = applyFunc(x) } } without any

In scala 2 or 3, is it possible to debug implicit resolution process in runtime?

偶尔善良 提交于 2020-02-23 06:27:51
问题 In scala language, implicit resolution is often done in compile-time and sometimes throws obfuscating error information, one famous example of such error is when shapeless Generic throws error information like: error: could not find implicit value for parameter encoder: CsvEncoder[Foo] (see https://books.underscore.io/shapeless-guide/shapeless-guide.html for detail) A solution to this problem is to run implicit resolution algorithm (should be a graph query algorithm internally) in runtime,

How to merge multiple imports in scala?

你离开我真会死。 提交于 2020-01-25 04:02:53
问题 Assume I have a library a.com. Everytime and in each file, I need to import a lot of package like import a.com._ import a.com.b._ import a.com.c import a.com.Implicits._ I don't want to write these code every time in each file of another project. Also if I want to change a.com to a.net , I have to change every file. Is there anyway to prevent this? 回答1: You can generate sources build.sbt lazy val commonSettings = Seq( scalaVersion := "2.13.1", ) lazy val in = project .settings( commonSettings

new-style (“inline”) macros require scala.meta

穿精又带淫゛_ 提交于 2020-01-03 10:21:16
问题 I just updated to scala meta 2.0.0-M1 and with the latest scala 2.12.3 and now macros no longer compile. The only change i made was to change the meta version from 1.8.0 to 2.0.0-M1. ERROR: new-style ("inline") macros require scala.meta Does anybody know if there is a quick work around for this? I was hoping to start playing with some of the semantic improvements. 回答1: scalameta/paradise currently supports scalameta 1.8.0 only, not 2.0.0-M1. The new improvements in the semantic api are not

How does one obtain the type of the value that an AST represents?

痴心易碎 提交于 2019-12-13 06:16:08
问题 I’m trying to write the following: import scala.reflect.runtime.universe._ val value: Tree = /* some AST */ val tpe = typeOf(value) // This should be the result type of the AST. // This is pseudocode. What should // actually go on this line? q""" type U = $tpe val v: U = $value """ I need to capture the type of the value represented by the AST value in tpe and assign it to U . How does one do this? Edit: Giving a type annotation for value and matching on it via quasiquotes isn't an option

automatically generate case object for case class

感情迁移 提交于 2019-12-13 03:16:39
问题 How can I have the scala compiler automatically generate the case object? // Pizza class class Pizza (val crust_type: String) // companion object object Pizza { val crustType = "crust_type" } Desired properties for case object for each attribute in the case class generate an attribute in the case object set the value in of each corresponding case object to the string representation of the attribute name and change camelCase to snake_case for the object attribute name, keep snake_case for

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

Compile Time Parameter for Macro Expansion

落花浮王杯 提交于 2019-12-11 05:29:47
问题 I would like to write an annotation macro that adds an extends <sometype> to traits where <sometype> can be specified at compile time. How can a compile time parameter be passed to a macro expansion? Ideally I would like to specify a command line argument at the compiler call. 回答1: Macro annotations don't have access to the command-line flags passed to scalac. However, one possible way to achieve this might be to use system properties. For example, in the macro annotation implementation //

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) =