forked from milessabin/shapeless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
derive.scala
590 lines (467 loc) · 15 KB
/
derive.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package shapeless
import org.junit.Test
object DeriveTestsDefinitions {
case class CC(i: Int, s: String = "b")
sealed trait Base
case class BaseI(i: Int) extends Base
case class BaseS(s: String) extends Base
case object BaseObj extends Base
object Simple {
trait TC[T] {
def repr: String
}
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](s: => String): TC[T] =
new TC[T] {
def repr = s
}
object typeClass {
def point[A]: TC[A] = instance(".")
def product[A, B](head: TC[A], tail: TC[B]): TC[(A, B)] =
instance(s"${head.repr},${tail.repr}")
def sum[A, B](head: TC[A], tail: => TC[B]): TC[Either[A, B]] =
instance(s"${head.repr}+${tail.repr}")
def map[F, G](tc: TC[G]): TC[F] =
instance(s"<${tc.repr}>")
}
implicit val intTC: TC[Int] = instance("int")
implicit val stringTC: TC[String] = instance("string")
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[TC[T]]]): TC[T] =
lzDerive.value.value
}
}
object PointArg {
trait TC[T] {
def repr: String
}
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](s: => String): TC[T] =
new TC[T] {
def repr = s
}
object typeClass {
def point[A](a: => A): TC[A] = instance(try a.toString catch { case _: Exception => "exception" })
def product[A, B](head: TC[A], tail: TC[B]): TC[(A, B)] =
instance(s"${head.repr},${tail.repr}")
def sum[A, B](head: TC[A], tail: => TC[B]): TC[Either[A, B]] =
instance(s"${head.repr}+${tail.repr}")
def map[F, G](tc: TC[G]): TC[F] =
instance(s"<${tc.repr}>")
}
implicit val intTC: TC[Int] = instance("int")
implicit val stringTC: TC[String] = instance("string")
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[TC[T]]]): TC[T] =
lzDerive.value.value
}
}
object Name {
trait TC[T] {
def repr: String
}
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](s: => String): TC[T] =
new TC[T] {
def repr = s
}
object typeClass {
def point[A]: TC[A] = instance(".")
def product[A, B](head: TC[A], tail: TC[B], name: String): TC[(A, B)] =
instance(s"$name:${head.repr},${tail.repr}")
def sum[A, B](head: TC[A], tail: => TC[B], name: String): TC[Either[A, B]] =
instance(s"$name:${head.repr}+${tail.repr}")
def map[F, G](tc: TC[G]): TC[F] =
instance(s"<${tc.repr}>")
}
implicit val intTC: TC[Int] = instance("int")
implicit val stringTC: TC[String] = instance("string")
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[TC[T]]]): TC[T] =
lzDerive.value.value
}
}
object PointImplicit {
trait Ev {
def value: String
}
trait TC[T] {
def repr: String
}
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](s: => String): TC[T] =
new TC[T] {
def repr = s
}
object typeClass {
def point[A](implicit ev: Ev): TC[A] = instance(ev.value)
def product[A, B](head: TC[A], tail: TC[B]): TC[(A, B)] =
instance(s"${head.repr},${tail.repr}")
def sum[A, B](head: TC[A], tail: => TC[B]): TC[Either[A, B]] =
instance(s"${head.repr}+${tail.repr}")
def map[F, G](tc: TC[G]): TC[F] =
instance(s"<${tc.repr}>")
}
implicit val intTC: TC[Int] = instance("int")
implicit val stringTC: TC[String] = instance("string")
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[TC[T]]]): TC[T] =
lzDerive.value.value
}
}
/** head and tail in product and sum have different type classes */
object HeadTailTC {
trait TC[T] {
def repr: String
}
trait LowPriorityTC {
implicit def fromSum[S](implicit tc: SumTC[S]): TC[S] =
TC.instance(tc.repr)
}
object TC extends LowPriorityTC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](s: => String): TC[T] =
new TC[T] {
def repr = s
}
implicit val intTC: TC[Int] = instance("int")
implicit val stringTC: TC[String] = instance("string")
implicit def fromProduct[P](implicit tc: ProductTC[P]): TC[P] =
instance(tc.repr)
}
trait ProductTC[T] { self =>
def start: String
def separator: String
def end: String
def items: Seq[String]
def repr = items.mkString(start, separator, end)
def prepend[U](item: String): ProductTC[U] =
new ProductTC[U] {
def start = self.start
def separator = self.separator
def end = self.end
def items = item +: self.items
}
def to[U]: ProductTC[U] =
new ProductTC[U] {
def start = "<"
def separator = self.separator
def end = ">"
def items = self.items
}
}
object ProductTC {
def apply[T](implicit tc: ProductTC[T]): ProductTC[T] = tc
def instance[T](start0: String, separator0: String, end0: String, items0: Seq[String]): ProductTC[T] =
new ProductTC[T] {
def start = start0
def separator = separator0
def end = end0
def items = items0
}
object typeClass {
def point[A]: ProductTC[A] = instance("", ",", "", Nil)
def product[A, B](head: TC[A], tail: ProductTC[B]): ProductTC[(A, B)] =
tail.prepend(head.repr)
def map[F, G](tc: ProductTC[G]): ProductTC[F] =
tc.to
}
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[ProductTC[T]]]): ProductTC[T] =
lzDerive.value.value
}
trait SumTC[T] { self =>
def start: String
def separator: String
def end: String
def items: Seq[String]
def repr = items.mkString(start, separator, end)
def prepend[U](item: String): SumTC[U] =
new SumTC[U] {
def start = self.start
def separator = self.separator
def end = self.end
def items = item +: self.items
}
def to[U]: SumTC[U] =
new SumTC[U] {
def start = "<"
def separator = self.separator
def end = ">"
def items = self.items
}
}
object SumTC {
def apply[T](implicit tc: SumTC[T]): SumTC[T] = tc
def instance[T](start0: String, separator0: String, end0: String, items0: Seq[String]): SumTC[T] =
new SumTC[T] {
def start = start0
def separator = separator0
def end = end0
def items = items0
}
object typeClass {
def point[A]: SumTC[A] = instance("", "+", "", Nil)
def sum[A, B](head: TC[A], tail: => SumTC[B]): SumTC[(A, B)] =
tail.prepend(head.repr)
def map[F, G](tc: SumTC[G]): SumTC[F] =
tc.to
}
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[SumTC[T]]]): SumTC[T] =
lzDerive.value.value
}
}
object Default {
trait TC[T] {
def repr: String
}
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](s: => String): TC[T] =
new TC[T] {
def repr = s
}
object typeClass {
def point[A]: TC[A] = instance(".")
def product[A, B](head: TC[A], tail: TC[B], default: Option[A]): TC[(A, B)] =
instance(s"${head.repr}${default.map("="+_).mkString},${tail.repr}")
def sum[A, B](head: TC[A], tail: => TC[B]): TC[Either[A, B]] =
instance(s"${head.repr}+${tail.repr}")
def map[F, G](tc: TC[G]): TC[F] =
instance(s"<${tc.repr}>")
}
implicit val intTC: TC[Int] = instance("int")
implicit val stringTC: TC[String] = instance("string")
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[TC[T]]]): TC[T] =
lzDerive.value.value
}
}
object From {
trait TC[T] {
def build: T
}
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](t: => T): TC[T] =
new TC[T] {
def build = t
}
object typeClass {
def point[A](a: => A): TC[A] = instance(a)
def product[A, B](head: TC[A], tail: TC[B]): TC[(A, B)] =
instance((head.build, tail.build))
def sum[A, B](head: TC[A], tail: => TC[B]): TC[Either[A, B]] =
instance(Left(head.build))
def map[F, G](tc: TC[G], from: G => F): TC[F] =
instance(from(tc.build))
}
implicit val intTC: TC[Int] = instance(1)
implicit val stringTC: TC[String] = instance("b")
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[TC[T]]]): TC[T] =
lzDerive.value.value
}
}
object To {
trait TC[T] {
def validate(t: T): Boolean
}
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](validate0: T => Boolean): TC[T] =
new TC[T] {
def validate(t: T) = validate0(t)
}
object typeClass {
// Doesn't work if the argument of point is removed here
def point[A](a: => A): TC[A] = instance(_ => true)
def product[A, B](head: TC[A], tail: TC[B]): TC[(A, B)] =
instance { case (h, t) => head.validate(h) && tail.validate(t) }
def sum[A, B](head: TC[A], tail: => TC[B]): TC[Either[A, B]] =
instance {
case Left(h) => head.validate(h)
case Right(t) => tail.validate(t)
}
def map[F, G](tc: TC[G], to: F => G): TC[F] =
instance(f => tc.validate(to(f)))
}
implicit val intTC: TC[Int] = instance(_ == 1)
implicit val stringTC: TC[String] = instance(_ == "b")
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[TC[T]]]): TC[T] =
lzDerive.value.value
}
}
object ProductImplicitArg {
trait TC0[T] {
def extra: String
}
object TC0 {
implicit val intTC0: TC0[Int] =
new TC0[Int] {
def extra = "0"
}
implicit val stringTC0: TC0[String] =
new TC0[String] {
def extra = "s"
}
}
trait TC[T] {
def repr: String
}
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](s: => String): TC[T] =
new TC[T] {
def repr = s
}
object typeClass {
def point[A]: TC[A] = instance(".")
def product[A, B](head: TC[A], tail: TC[B])(implicit tc0: TC0[A]): TC[(A, B)] =
instance(s"${head.repr}${tc0.extra},${tail.repr}")
def sum[A, B](head: TC[A], tail: => TC[B]): TC[Either[A, B]] =
instance(s"${head.repr}+${tail.repr}")
def map[F, G](tc: TC[G]): TC[F] =
instance(s"<${tc.repr}>")
}
implicit val intTC: TC[Int] = instance("int")
implicit val stringTC: TC[String] = instance("string")
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[TC[T]]]): TC[T] =
lzDerive.value.value
}
}
object SumImplicitArg {
trait TC0[T] {
def extra: String
}
object TC0 {
implicit val baseITC0: TC0[BaseI] =
new TC0[BaseI] {
def extra = "1"
}
implicit val baseSTC0: TC0[BaseS] =
new TC0[BaseS] {
def extra = "2"
}
implicit val baseObjTC0: TC0[BaseObj.type] =
new TC0[BaseObj.type] {
def extra = "3"
}
}
trait TC[T] {
def repr: String
}
object TC {
def apply[T](implicit tc: TC[T]): TC[T] = tc
def instance[T](s: => String): TC[T] =
new TC[T] {
def repr = s
}
object typeClass {
def point[A]: TC[A] = instance(".")
def product[A, B](head: TC[A], tail: TC[B]): TC[(A, B)] =
instance(s"${head.repr},${tail.repr}")
def sum[A, B](head: TC[A], tail: => TC[B])(implicit tc0: TC0[A]): TC[Either[A, B]] =
instance(s"${head.repr}${tc0.extra}+${tail.repr}")
def map[F, G](tc: TC[G]): TC[F] =
instance(s"<${tc.repr}>")
}
implicit val intTC: TC[Int] = instance("int")
implicit val stringTC: TC[String] = instance("string")
implicit def derivedTC[T](implicit lzDerive: Lazy[Derive[TC[T]]]): TC[T] =
lzDerive.value.value
}
}
}
class DeriveTests {
import DeriveTestsDefinitions._
@Test
def simple {
import Simple._
assert(TC[Int].repr == "int")
assert(TC[String].repr == "string")
assert(TC[CC].repr == "<int,string,.>")
assert(TC[Base].repr == "<<int,.>+<.>+<string,.>+.>")
}
@Test
def pointArg {
import PointArg._
assert(TC[Int].repr == "int")
assert(TC[String].repr == "string")
assert(TC[CC].repr == "<int,string,()>")
assert(TC[Base].repr == "<<int,()>+<()>+<string,()>+exception>")
}
@Test
def name {
import Name._
assert(TC[Int].repr == "int")
assert(TC[String].repr == "string")
assert(TC[CC].repr == "<i:int,s:string,.>")
assert(TC[Base].repr == "<BaseI:<i:int,.>+BaseObj:<.>+BaseS:<s:string,.>+.>")
}
@Test
def pointImplicit {
import PointImplicit._
implicit val ev =
new Ev {
def value = "0"
}
assert(TC[Int].repr == "int")
assert(TC[String].repr == "string")
assert(TC[CC].repr == "<int,string,0>")
assert(TC[Base].repr == "<<int,0>+<0>+<string,0>+0>")
}
@Test
def headTailTC {
import HeadTailTC._
assert(TC[Int].repr == "int")
assert(TC[String].repr == "string")
assert(TC[CC].repr == "<int,string>")
assert(TC[Base].repr == "<<int>+<>+<string>>")
}
@Test
def default {
import Default._
assert(TC[Int].repr == "int")
assert(TC[String].repr == "string")
assert(TC[CC].repr == "<int,string=b,.>")
assert(TC[Base].repr == "<<int,.>+<.>+<string,.>+.>")
}
@Test
def from {
import From._
assert(TC[Int].build == 1)
assert(TC[String].build == "b")
assert(TC[CC].build == CC(1, "b"))
assert(TC[Base].build == BaseI(1))
}
@Test
def to {
import To._
assert(!TC[Int].validate(0))
assert(TC[Int].validate(1))
assert(!TC[String].validate(""))
assert(TC[String].validate("b"))
assert(!TC[CC].validate(CC(1, "")))
assert(!TC[CC].validate(CC(0, "b")))
assert(TC[CC].validate(CC(1, "b")))
assert(!TC[Base].validate(BaseI(0)))
assert(TC[Base].validate(BaseI(1)))
assert(!TC[Base].validate(BaseS("")))
assert(TC[Base].validate(BaseS("b")))
assert(TC[Base].validate(BaseObj))
}
@Test
def productImplicitArg {
import ProductImplicitArg._
assert(TC[Int].repr == "int")
assert(TC[String].repr == "string")
assert(TC[CC].repr == "<int0,strings,.>")
assert(TC[Base].repr == "<<int0,.>+<.>+<strings,.>+.>")
}
@Test
def sumImplicitArg {
import SumImplicitArg._
assert(TC[Int].repr == "int")
assert(TC[String].repr == "string")
assert(TC[CC].repr == "<int,string,.>")
assert(TC[Base].repr == "<<int,.>1+<.>3+<string,.>2+.>")
}
}