@@ -166,6 +166,38 @@ object Pair extends Deriving {
166
166
// two clauses that could be generated from a `derives` clause
167
167
implicit def PairEq [T : Eq ]: Eq [Pair [T ]] = Eq .derived
168
168
implicit def PairPickler [T : Pickler ]: Pickler [Pair [T ]] = Pickler .derived
169
+ implicit def PairShow [T : Show ]: Show [Pair [T ]] = Show .derived
170
+ }
171
+
172
+ sealed trait Either [+ L , + R ] extends Product derives Eq , Pickler
173
+ case class Left [L ](x : L ) extends Either [L , Nothing ]
174
+ case class Right [R ](x : R ) extends Either [Nothing , R ]
175
+
176
+ object Either extends Deriving {
177
+ import Deriving ._
178
+
179
+ type Shape [L , R ] = Shape .Cases [(
180
+ Shape .Case [Left [L ], L *: Unit ],
181
+ Shape .Case [Right [R ], R *: Unit ]
182
+ )]
183
+
184
+ implicit def eitherShape [L , R ]: Shaped [Either [L , R ], Shape [L , R ]] = new {
185
+ def reflect (e : Either [L , R ]): Mirror = e match {
186
+ case e : Left [L ] => mirror(0 , e)
187
+ case e : Right [R ] => mirror(1 , e)
188
+ }
189
+ def reify (c : Mirror ): Either [L , R ] = c.ordinal match {
190
+ case 0 => Left [L ](c(0 ).asInstanceOf )
191
+ case 1 => Right [R ](c(0 ).asInstanceOf )
192
+ }
193
+ def deriving = Either
194
+ }
195
+
196
+ protected val caseLabels = Array (" Left\000 x" , " Right\000 x" )
197
+
198
+ implicit def EitherEq [L : Eq , R : Eq ]: Eq [Either [L , R ]] = Eq .derived
199
+ implicit def EitherPickler [L : Pickler , R : Pickler ]: Pickler [Either [L , R ]] = Pickler .derived
200
+ implicit def EitherShow [L : Show , R : Show ]: Show [Either [L , R ]] = Show .derived
169
201
}
170
202
171
203
// A typeclass
@@ -174,7 +206,7 @@ trait Eq[T] {
174
206
}
175
207
176
208
object Eq {
177
- import scala .typelevel ._
209
+ import _root_ . scala .typelevel ._
178
210
import Deriving ._
179
211
180
212
inline def tryEql [T ](x : T , y : T ) = implicit match {
@@ -423,4 +455,25 @@ object Test extends App {
423
455
println(implicitly[Show [T ]].show(x))
424
456
showPrintln(xs)
425
457
showPrintln(xss)
458
+
459
+ val zs = Lst .Cons (Left (1 ), Lst .Cons (Right (Pair (2 , 3 )), Lst .Nil ))
460
+ showPrintln(zs)
461
+
462
+ def pickle [T : Pickler ](buf : mutable.ListBuffer [Int ], x : T ): Unit =
463
+ implicitly[Pickler [T ]].pickle(buf, x)
464
+
465
+ def unpickle [T : Pickler ](buf : mutable.ListBuffer [Int ]): T =
466
+ implicitly[Pickler [T ]].unpickle(buf)
467
+
468
+ def copy [T : Pickler ](x : T ): T = {
469
+ val buf = new mutable.ListBuffer [Int ]
470
+ pickle(buf, x)
471
+ unpickle[T ](buf)
472
+ }
473
+
474
+ def eql [T : Eq ](x : T , y : T ) = implicitly[Eq [T ]].eql(x, y)
475
+
476
+ val zs1 = copy(zs)
477
+ showPrintln(zs1)
478
+ assert(eql(zs, zs1))
426
479
}
0 commit comments