-
Notifications
You must be signed in to change notification settings - Fork 531
/
shows.scala
114 lines (91 loc) · 2.91 KB
/
shows.scala
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) 2013-14 Lars Hupel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package shapeless.examples
import shapeless._
object ShowExamples extends App {
import ShowSyntax._
sealed trait Super
case class Foo(i: Int, s: String) extends Super
case class Bar(i: Int) extends Super
case class BarRec(i: Int, rec: Super) extends Super
object Super {
implicit val instance = Show[Super]
}
sealed trait MutualA
case class MutualA1(x: Int) extends MutualA
case class MutualA2(b: MutualB) extends MutualA
sealed trait MutualB
case class MutualB1(x: Int) extends MutualB
case class MutualB2(b: MutualA) extends MutualB
object MutualA {
implicit val aInstance = Show[MutualA]
}
object MutualB {
implicit val bInstance = Show[MutualB]
}
val bar: Super = Bar(0)
val rec: Super = BarRec(1, Foo(0, "foo"))
assert(bar.show == "Bar(i = 0)")
assert(rec.show == "BarRec(i = 1, rec = Foo(i = 0, s = foo))")
val mutual: MutualA = MutualA2(MutualB2(MutualA1(0)))
assert(mutual.show == "MutualA2(b = MutualB2(b = MutualA1(x = 0)))")
}
trait ShowSyntax {
def show: String
}
object ShowSyntax {
implicit def showSyntax[T](a: T)(implicit st: Show[T]): ShowSyntax = new ShowSyntax {
def show = st.show(a)
}
}
trait Show[T] {
def show(t: T): String
}
object Show extends LabelledTypeClassCompanion[Show] {
implicit def stringShow: Show[String] = new Show[String] {
def show(t: String) = t
}
implicit def intShow: Show[Int] = new Show[Int] {
def show(n: Int) = n.toString
}
object typeClass extends LabelledTypeClass[Show] {
def emptyProduct = new Show[HNil] {
def show(t: HNil) = ""
}
def product[F, T <: HList](name: String, sh: Show[F], st: Show[T]) = new Show[F :: T] {
def show(ft: F :: T) = {
val head = sh.show(ft.head)
val tail = st.show(ft.tail)
if (tail.isEmpty)
s"$name = $head"
else
s"$name = $head, $tail"
}
}
def emptyCoproduct = new Show[CNil] {
def show(t: CNil) = ""
}
def coproduct[L, R <: Coproduct](name: String, sl: => Show[L], sr: => Show[R]) = new Show[L :+: R] {
def show(lr: L :+: R) = lr match {
case Inl(l) => s"$name(${sl.show(l)})"
case Inr(r) => s"${sr.show(r)}"
}
}
def project[F, G](instance: => Show[G], to: F => G, from: G => F) = new Show[F] {
def show(f: F) = instance.show(to(f))
}
}
}