|
| 1 | +/* These tests show various mechanisms available for implicit prioritization. |
| 2 | + */ |
| 3 | + |
| 4 | +class E[T](val str: String) // The type for which we infer terms below |
| 5 | + |
| 6 | +class Arg[T] // An argument that we use as a given for some implied instances below |
| 7 | + |
| 8 | +/* First, two schemes that require a pre-planned architecture for how and |
| 9 | + * where implied instances are defined. |
| 10 | + * |
| 11 | + * Traditional scheme: prioritize with location in class hierarchy |
| 12 | + */ |
| 13 | +class LowPriorityImplicits { |
| 14 | + implied t1[T] for E[T]("low") |
| 15 | +} |
| 16 | + |
| 17 | +object NormalImplicits extends LowPriorityImplicits { |
| 18 | + implied t2[T] given Arg[T] for E[T]("norm") |
| 19 | +} |
| 20 | + |
| 21 | +def test1 = { |
| 22 | + import implied NormalImplicits._ |
| 23 | + assert(the[E[String]].str == "low") // No Arg available, so only t1 applies |
| 24 | + |
| 25 | + { implied for Arg[String] |
| 26 | + assert(the[E[String]].str == "norm") // Arg available, t2 takes priority |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/* New scheme: dummy implicit arguments that indicate priorities |
| 31 | + */ |
| 32 | +object Priority { |
| 33 | + class Low |
| 34 | + object Low { implied for Low } |
| 35 | + |
| 36 | + class High extends Low |
| 37 | + object High { implied for High } |
| 38 | +} |
| 39 | + |
| 40 | +object Impl2 { |
| 41 | + implied t1[T] given Priority.Low for E[T]("low") |
| 42 | + implied t2[T] given Priority.High given Arg[T] for E[T]("norm") |
| 43 | +} |
| 44 | + |
| 45 | +def test2 = { |
| 46 | + import implied Impl2._ |
| 47 | + assert(the[E[String]].str == "low") // No Arg available, so only t1 applies |
| 48 | + |
| 49 | + { implied for Arg[String] |
| 50 | + assert(the[E[String]].str == "norm") // Arg available, t2 takes priority |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/* The remaining tests show how we can add an override of highest priority or |
| 55 | + * a fallback of lowest priority to a group of existing implied instances, without |
| 56 | + * needing to change the location or definition of those instances. |
| 57 | + * |
| 58 | + * First, consider the problem how to define an override of highest priority. |
| 59 | + * If all of the alternatives in the existing hierarchy take implicit arguments, |
| 60 | + * an alternative without implicit arguments would override all of them. |
| 61 | + */ |
| 62 | +object Impl2a { |
| 63 | + implied t3[T] for E[T]("hi") |
| 64 | +} |
| 65 | + |
| 66 | +def test2a = { |
| 67 | + import implied Impl2._ |
| 68 | + import implied Impl2a._ |
| 69 | + |
| 70 | + implied for Arg[String] |
| 71 | + assert(the[E[String]].str == "hi") |
| 72 | +} |
| 73 | + |
| 74 | +/* If that solution is not applicable, we can define an override by refining the |
| 75 | + * result type of the implied instance, e.g. like this: |
| 76 | + */ |
| 77 | +object Impl3 { |
| 78 | + implied t1[T] for E[T]("low") |
| 79 | +} |
| 80 | + |
| 81 | +object Override { |
| 82 | + trait HighestPriority // A marker trait to indicate a higher priority |
| 83 | + |
| 84 | + implied over[T] for E[T]("hi"), HighestPriority |
| 85 | +} |
| 86 | + |
| 87 | +def test3 = { |
| 88 | + import implied Impl3._ |
| 89 | + assert(the[E[String]].str == "low") // only t1 is available |
| 90 | + |
| 91 | + { import implied Override._ |
| 92 | + import implied Impl3._ |
| 93 | + assert(the[E[String]].str == "hi") // `over` takes priority since its result type is a subtype of t1's. |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/* Now consider the dual problem: How to install a fallback with lower priority than existing |
| 98 | + * implied instances that kicks in when none of the other instances are applicable. |
| 99 | + * We get there in two stages. The first stage is by defining an explicit `withFallback` method |
| 100 | + * that takes the right implicit and returns it. This can be achieved using an implicit parameter |
| 101 | + * with a default argument. |
| 102 | + */ |
| 103 | +object Impl4 { |
| 104 | + implied t1 for E[String]("string") |
| 105 | + implied t2[T] given Arg[T] for E[T]("generic") |
| 106 | +} |
| 107 | + |
| 108 | +object fallback4 { |
| 109 | + def withFallback[T] given (ev: E[T] = new E[T]("fallback")): E[T] = ev |
| 110 | +} |
| 111 | + |
| 112 | +def test4 = { |
| 113 | + import implied Impl4._ |
| 114 | + import fallback4._ |
| 115 | + assert(withFallback[String].str == "string") // t1 is applicable |
| 116 | + assert(withFallback[Int].str == "fallback") // No applicable instances, pick the default |
| 117 | + |
| 118 | + { implied for Arg[Int] |
| 119 | + assert(withFallback[Int].str == "generic") // t2 is applicable |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/* The final setup considers the problem how to define a fallback with lower priority than existing |
| 124 | + * implicits that exists as an implicit instance alongside the others. This can be achieved |
| 125 | + * by combining the implicit parameter with default technique for getting an existing impplicit |
| 126 | + * or a fallback with the result refinement technique for overriding all existing implicit instances. |
| 127 | + * |
| 128 | + * It employs a more re-usable version of the result refinement trick. |
| 129 | + */ |
| 130 | +opaque type HigherPriority = Any |
| 131 | +object HigherPriority { |
| 132 | + def inject[T](x: T): T & HigherPriority = x |
| 133 | +} |
| 134 | + |
| 135 | +object fallback5 { |
| 136 | + implied [T] given (ev: E[T] = new E[T]("fallback")) for (E[T] & HigherPriority) = HigherPriority.inject(ev) |
| 137 | +} |
| 138 | + |
| 139 | +def test5 = { |
| 140 | + import implied Impl4._ |
| 141 | + import implied fallback5._ |
| 142 | + |
| 143 | + // All inferred terms go through the implied instance in fallback5. |
| 144 | + // They differ in what implicit argument is synthesized for that instance. |
| 145 | + assert(the[E[String]].str == "string") // t1 is applicable |
| 146 | + assert(the[E[Int]].str == "fallback") // No applicable instances, pick the default |
| 147 | + |
| 148 | + { implied for Arg[Int] |
| 149 | + assert(the[E[Int]].str == "generic") // t2 is applicable |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +object Test extends App { |
| 154 | + test1 |
| 155 | + test2 |
| 156 | + test2a |
| 157 | + test3 |
| 158 | + test4 |
| 159 | + test5 |
| 160 | +} |
| 161 | + |
0 commit comments