case class Customer(name: String, yearOfBirth: Int)
object Customer {
def toMap(c: Customer): Map[String, Any] = ???
}
@mappable case class Customer(name: String, yearOfBirth: Int)
class mappable extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
defn
}
}
// Definition of object, class, type etc.
trait Defn.Object(mods: Seq[Mod], name: Term.Name,
templ: Template)
trait Defn.Class(mods: Seq[Mod], name: Type.Name,
tparams: Seq[Type.Param], ctor: Ctor.Primary,
templ: Template)
trait Defn.Type(mods: Seq[Mod], name: Type.Name,
tparams: Seq[Type.Param], body: Type)
// Different namespaces for names
trait Type.Name(value: String) // type name, e.g. List.type
trait Term.Name(value: String) // expression, e.g. List companion object
val clazz = q"case class Foo(i: Int)" //construction
clazz match { // deconstruction
case q"""..$mods class $tname[..$tparams]
..$ctorMods (...$paramss) extends $template""" ⇒
}
q"""object MyObject {
$clazz //Defn.Class
..$otherClasses //Seq[Defn.Class]
}"""
q"List" //Term.Name: List companion object
t"List" //Type.Name: List type
import scala.meta.serialiser._
object Foo { def apple = "bar"}
@mappable case class Foo[A](
i: Option[Int] = Some(42),
@mappedTo("aMapped") a: A,
@nullable s: String) {
def banana = i.get
}
Foo.toMap(Foo(a = true, s = "bar"))
// Map(i -> 42, aMapped -> true, s -> bar)
Foo.fromMap(Map("i" -> 42, "aMapped" -> true, "s" -> "bar"))
Foo.fromMap(Map("aMapped" -> true))
github.com/mpollmeier/scalameta-serialiser
@myMacro("fooValue") class MyClass
class myMacro(foo: String) extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
println(foo) // error: value not found
this match {
case q"new $_()" ⇒ // no params defined
case q"new $_(params))" ⇒
}
}
}
@mappable case class CC(@nullable s: String)
class nullable extends StaticAnnotation
// no inline/meta block allowed, but likely not necessary
sbt console
[error] error while loading Object,
[error] Missing dependency 'object scala in compiler mirror'
Workaround: factor out generated code into separate project
Discussion at scalameta/paradise #10