|
| 1 | +import dotty.{Tuple, TupleCons, LargeTuple} |
| 2 | +import dotty.{TupleCons => ::} |
| 3 | + |
| 4 | +// Type level natural numbers --------------------------------------------------------------------- |
| 5 | + |
| 6 | +sealed trait Nat |
| 7 | +sealed trait Succ[P <: Nat] extends Nat |
| 8 | +sealed trait Zero extends Nat |
| 9 | + |
| 10 | +// Accessor type class to compute the N'th element of an Tuple L ---------------------------------- |
| 11 | + |
| 12 | +trait At[L <: Tuple, N <: Nat, Out] { |
| 13 | + def apply(l: L): Out |
| 14 | +} |
| 15 | + |
| 16 | +object At { |
| 17 | + implicit def caseZero[H, T <: Tuple]: At[H :: T, Zero, H] = |
| 18 | + new At[H :: T, Zero, H] { |
| 19 | + def apply(l: H :: T): H = { |
| 20 | + val TupleCons(h, _) = l |
| 21 | + h |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + implicit def caseN[H, T <: Tuple, N <: Nat, O] |
| 26 | + (implicit a: At[T, N, O]): At[H :: T, Succ[N], O] = |
| 27 | + new At[H :: T, Succ[N], O] { |
| 28 | + def apply(l: H :: T): O = { |
| 29 | + val TupleCons(_, t) = l |
| 30 | + a(t) |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +// An HMap is an Tuple with HEntry elements. We are reusing Tuple for it's nice syntax |
| 37 | + |
| 38 | +// Should be `K <: Singleton` but inference for `--` does not quite work with that. |
| 39 | +final case class HEntry[K, V](value: V) |
| 40 | + |
| 41 | +// Accessor type class to compute the element of type K in a HMap L ------------------------------- |
| 42 | + |
| 43 | +trait PhantomGet[K, M <: Tuple, I <: Nat] // extends PhantomAny |
| 44 | + |
| 45 | +object PhantomGet { |
| 46 | + implicit def getHead[K, V, T <: Tuple] |
| 47 | + : PhantomGet[K, HEntry[K, V] :: T, Zero] = null |
| 48 | + |
| 49 | + implicit def getTail[K, H, T <: Tuple, I <: Nat] |
| 50 | + (implicit t: PhantomGet[K, T, I]) |
| 51 | + : PhantomGet[K, H :: T, Succ[I]] = null |
| 52 | +} |
| 53 | + |
| 54 | +// Syntax ----------------------------------------------------------------------------------------- |
| 55 | + |
| 56 | +object syntax { |
| 57 | + object hmap { |
| 58 | + implicit class hmapGet[M <: Tuple](m: M) { |
| 59 | + def get[K, V, I <: Nat](k: K) |
| 60 | + (implicit |
| 61 | + g: PhantomGet[k.type, M, I], |
| 62 | + a: At[M, I, HEntry[k.type, V]] |
| 63 | + ): V = a(m).value |
| 64 | + } |
| 65 | + |
| 66 | + implicit class EntryAssoc[K](k: K) { |
| 67 | + def -- [V](value: V): HEntry[K, V] = HEntry(value) |
| 68 | + } |
| 69 | + |
| 70 | + type --[A, B] = HEntry[A, B] |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +object Test { |
| 75 | + def main(args: Array[String]): Unit = { |
| 76 | + import syntax.hmap._ |
| 77 | + |
| 78 | + type MapType = ( |
| 79 | + "name" -- String, |
| 80 | + "genre" -- Boolean, |
| 81 | + "moneyz" -- Int, |
| 82 | + "cat" -- String |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | + val f = HEntry[K = "name"]("foo") |
| 87 | + val l = HEntry[K = "genre"](true) |
| 88 | + val i = HEntry[K = "moneyz"](123) |
| 89 | + val b = HEntry[K = "cat"]("bar") |
| 90 | + |
| 91 | + val map: MapType = TupleCons(f, TupleCons(l, TupleCons(i, TupleCons(b, ())))) |
| 92 | + |
| 93 | + |
| 94 | + assert(map.get("name") == "foo") |
| 95 | + assert(map.get("genre") == true) |
| 96 | + assert(map.get("moneyz") == 123) |
| 97 | + assert(map.get("cat") == "bar") |
| 98 | + |
| 99 | + val map2: MapType = ( |
| 100 | + "name" -- "foo", |
| 101 | + "genre" -- true, |
| 102 | + "moneyz" -- 123, |
| 103 | + "cat" -- "bar" |
| 104 | + ) |
| 105 | + |
| 106 | + assert(map2.get("name") == "foo") |
| 107 | + assert(map2.get("genre") == true) |
| 108 | + assert(map2.get("moneyz") == 123) |
| 109 | + assert(map2.get("cat") == "bar") |
| 110 | + } |
| 111 | +} |
0 commit comments