blob: 7199b13863b6472896c1b568b67a2cccebb1f600 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package magnolia.examples
import magnolia._
import language.experimental.macros
import language.higherKinds
import collection.immutable.ListMap
object `package` {
implicit class Showable[T: Show](t: T) {
def show: String = implicitly[Show[T]].show(t)
}
implicit val showString: Show[String] = identity
implicit val showBool: Show[Boolean] = _.toString
implicit def showList[T: Show]: Show[List[T]] = xs => xs.map { x => s"list:${implicitly[Show[T]].show(x)}" }.mkString(";")
implicit def showSet[T: Show]: Show[Set[T]] = s => "set"
}
sealed trait Tree
case class Branch(left: Tree, right: Tree) extends Tree
case class Leaf(value: Int) extends Tree
sealed trait Entity
case class Person(name: String, address: Address) extends Entity
case class Organization(name: String, contacts: Set[Person]) extends Entity
case class Address(lines: List[String], country: Country)
case class Country(name: String, code: String, salesTax: Boolean)
trait Show[T] { def show(t: T): String }
object Show extends Show_1 {
implicit val showInt: Show[Int] = _.toString
implicit val derivation = new ContravariantDerivation[Show] {
type Return = String
def call[T](show: Show[T], value: T): String = show.show(value)
def construct[T](body: T => String): Show[T] = body(_)
def join(xs: ListMap[String, String]): String = xs.map { case (k, v) => s"$k=$v" }.mkString("{", ", ", "}")
}
}
trait Show_1 {
implicit def generic[T]: Show[T] = macro Macros.magnolia[T, Show[_]]
}
|