Skip to content

Commit 5ff22b2

Browse files
committed
Replace inheritance with ifdef for version specific Eithered
1 parent 697d9cb commit 5ff22b2

File tree

5 files changed

+189
-174
lines changed

5 files changed

+189
-174
lines changed

Base/src/main/scala-2/typeclass/VersionSpecificEithered.scala

Lines changed: 0 additions & 91 deletions
This file was deleted.

Base/src/main/scala-3/typeclass/VersionSpecificEithered.scala

Lines changed: 0 additions & 80 deletions
This file was deleted.

Base/src/main/scala/typeclass/Eithered.scala

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package name.rayrobdod.stringContextParserCombinator
22
package typeclass
33

4+
import com.eed3si9n.ifdef.ifdef
5+
46
/**
57
* Describes how to represent a result that may be one of two results
68
*
@@ -66,7 +68,7 @@ trait BiEithered[Expr[_], A, B, Z]
6668
* Predefined implicit implementations of Eithered
6769
* and methods to create new Eithereds
6870
*/
69-
object Eithered extends VersionSpecificEithered {
71+
object Eithered extends LowPrioEithered {
7072
/**
7173
* Constructs an `Eithered` from a set of functions corresponding to each of Eithered's methods
7274
*/
@@ -78,6 +80,46 @@ object Eithered extends VersionSpecificEithered {
7880
new Apply()
7981
}
8082

83+
@ifdef("scalaEpochVersion:2")
84+
implicit def unitUnit:Eithered[Unit, Unit, Unit] = symmetric[Unit]
85+
@ifdef("scalaBinaryVersion:3")
86+
implicit def unitUnit:Eithered[Unit, Unit, Unit] = Eithered.generic
87+
88+
@ifdef("scalaEpochVersion:2")
89+
implicit def unitGeneric[B, Z](implicit ev:Optionally[B, Z]):Eithered[Unit, B, Z] = Eithered(_ => ev.none, ev.some _)
90+
@ifdef("scalaEpochVersion:2")
91+
implicit def genericUnit[A, Z](implicit ev:Optionally[A, Z]):Eithered[A, Unit, Z] = Eithered(ev.some _, _ => ev.none)
92+
93+
@ifdef("scalaBinaryVersion:3")
94+
implicit def unitAny[B, Z](implicit ev:Optionally[B, Z]):Eithered[Unit, B, Z] = Eithered(_ => ev.none, ev.some _)
95+
@ifdef("scalaBinaryVersion:3")
96+
implicit def anyUnit[A, Z](implicit ev:Optionally[A, Z]):Eithered[A, Unit, Z] = Eithered(ev.some _, _ => ev.none)
97+
98+
/**
99+
* @version 0.1.1
100+
*/
101+
@ifdef("scalaEpochVersion:2")
102+
trait Eithereds[Expr[+_]] {
103+
def splicePiece[A]: Eithered[Expr[A], Expr[Iterable[A]], Repeated.SplicePiece[Expr, A]]
104+
}
105+
/**
106+
* @version 0.1.1
107+
*/
108+
@ifdef("scalaEpochVersion:2")
109+
def forContext(c:scala.reflect.macros.blackbox.Context):Eithereds[c.Expr] = {
110+
new Eithereds[c.Expr] {
111+
def splicePiece[A]: Eithered[c.Expr[A], c.Expr[Iterable[A]], Repeated.SplicePiece[c.Expr, A]] =
112+
Eithered(new Repeated.SplicePiece.One(_), new Repeated.SplicePiece.Many(_))
113+
}
114+
}
115+
116+
/**
117+
* @version 0.1.1
118+
*/
119+
@ifdef("scalaBinaryVersion:3")
120+
def quotedSplicePiece[A]: Eithered[scala.quoted.Expr[A], scala.quoted.Expr[Iterable[A]], Repeated.SplicePiece[scala.quoted.Expr, A]] =
121+
Eithered(new Repeated.SplicePiece.One(_), new Repeated.SplicePiece.Many(_))
122+
81123
/**
82124
* An Eithered that wraps the value in a `scala.Either`
83125
@@ -98,11 +140,27 @@ object Eithered extends VersionSpecificEithered {
98140
def discriminatedUnion[A, B]:Eithered[A, B, Either[A, B]] = Eithered(Left.apply _, Right.apply _)
99141
}
100142

143+
private[typeclass] trait LowPrioEithered {
144+
@ifdef("scalaEpochVersion:2")
145+
implicit def symmetric[A]:Eithered[A, A, A] = Eithered(Predef.identity _, Predef.identity _)
146+
147+
/**
148+
* The fallback Eithered;
149+
* creates a union type of the two component types.
150+
*
151+
* Since the union of a type with itself is equivalent to that same type,
152+
* if this Eithered is used for two parsers of the same type,
153+
* then the result is a parser of that type.
154+
*/
155+
@ifdef("scalaBinaryVersion:3")
156+
implicit def generic[A, B]:Eithered[A, B, A | B] = Eithered.apply(Predef.identity _, Predef.identity _)
157+
}
158+
101159
/**
102160
* Predefined implicit implementations of ContraEithered
103161
* and methods to create new ContraEithereds
104162
*/
105-
object ContraEithered extends VersionSpecificContraEithered {
163+
object ContraEithered extends LowPrioContraEithered {
106164
/**
107165
* Constructs an `ContraEithered` from a set of functions corresponding to each of ContraEithered's methods
108166
*/
@@ -116,13 +174,45 @@ object ContraEithered extends VersionSpecificContraEithered {
116174
}
117175
new Apply()
118176
}
177+
178+
@ifdef("scalaEpochVersion:2")
179+
trait ContraEithereds[Expr[_]] extends LowPrioContraEithereds[Expr] {
180+
implicit def unitUnit:ContraEithered[Expr, Unit, Unit, Unit]
181+
}
182+
@ifdef("scalaEpochVersion:2")
183+
private[typeclass]
184+
trait LowPrioContraEithereds[Expr[_]] {
185+
implicit def symmetric[A]:ContraEithered[Expr, A, A, A]
186+
}
187+
188+
@ifdef("scalaEpochVersion:2")
189+
def forContext(c:scala.reflect.macros.blackbox.Context):ContraEithereds[c.Expr] = {
190+
val backing = BiEithered.forContext(c)
191+
192+
new ContraEithereds[c.Expr] {
193+
implicit override def unitUnit:ContraEithered[c.Expr, Unit, Unit, Unit] = backing.unitUnit
194+
implicit override def symmetric[A]:ContraEithered[c.Expr, A, A, A] = backing.symmetric[A]
195+
}
196+
}
197+
198+
@ifdef("scalaBinaryVersion:3")
199+
implicit def quotedUnitUnit(implicit quotes:scala.quoted.Quotes):ContraEithered[scala.quoted.Expr, Unit, Unit, Unit] = quotedSymmetric[Unit]
200+
201+
implicit def idUnitUnit:ContraEithered[Id, Unit, Unit, Unit] = idSymmetric[Unit]
202+
}
203+
204+
private[typeclass] trait LowPrioContraEithered {
205+
@ifdef("scalaBinaryVersion:3")
206+
implicit def quotedSymmetric[A](implicit quotes:scala.quoted.Quotes):ContraEithered[scala.quoted.Expr, A, A, A] = BiEithered.quotedSymmetric
207+
208+
implicit def idSymmetric[A]:ContraEithered[Id, A, A, A] = BiEithered.idSymmetric
119209
}
120210

121211
/**
122212
* Predefined implicit implementations of BiEithered
123213
* and methods to create new BiEithereds
124214
*/
125-
object BiEithered extends VersionSpecificBiEithered {
215+
object BiEithered extends LowPrioBiEithered {
126216
/**
127217
* Constructs an `BiEithered` from a set of functions corresponding to each of BiEithered's methods
128218
*/
@@ -142,4 +232,76 @@ object BiEithered extends VersionSpecificBiEithered {
142232
new Apply()
143233
}
144234

235+
@ifdef("scalaEpochVersion:2")
236+
trait BiEithereds[Expr[_]] extends LowPrioBiEithereds[Expr] {
237+
implicit def unitUnit:BiEithered[Expr, Unit, Unit, Unit]
238+
}
239+
@ifdef("scalaEpochVersion:2")
240+
private[typeclass]
241+
trait LowPrioBiEithereds[Expr[_]] {
242+
implicit def symmetric[A]:BiEithered[Expr, A, A, A]
243+
}
244+
@ifdef("scalaEpochVersion:2")
245+
def forContext(c:scala.reflect.macros.blackbox.Context):BiEithereds[c.Expr] = {
246+
new BiEithereds[c.Expr] {
247+
override def unitUnit:BiEithered[c.Expr, Unit, Unit, Unit] = this.symmetric[Unit]
248+
249+
implicit override def symmetric[A]:BiEithered[c.Expr, A, A, A] = {
250+
val exprTrue = c.Expr[Boolean](c.universe.Liftable.liftBoolean(true))
251+
252+
BiEithered.apply[c.Expr, A, A, A](
253+
Predef.identity _,
254+
Predef.identity _,
255+
PartialExprFunction.identity(exprTrue),
256+
PartialExprFunction.identity(exprTrue)
257+
)
258+
}
259+
}
260+
}
261+
262+
@ifdef("scalaBinaryVersion:3")
263+
implicit def quotedUnitUnit(implicit quotes:scala.quoted.Quotes):BiEithered[scala.quoted.Expr, Unit, Unit, Unit] = quotedSymmetric[Unit]
264+
265+
@ifdef("scalaBinaryVersion:3")
266+
implicit def eitherUnitAny[Expr[_], B, Z](implicit ev:BiOptionally[Expr, B, Z]):BiEithered[Expr, Unit, B, Z] = BiEithered(
267+
_ => ev.none,
268+
ev.some _,
269+
PartialExprFunction[Expr, Z, Unit](
270+
ev.contraNone,
271+
_ => ()
272+
),
273+
ev.contraSome,
274+
)
275+
@ifdef("scalaBinaryVersion:3")
276+
implicit def eitherAnyUnit[Expr[_], A, Z](implicit ev:BiOptionally[Expr, A, Z]):BiEithered[Expr, A, Unit, Z] = BiEithered(
277+
ev.some _,
278+
_ => ev.none,
279+
ev.contraSome,
280+
PartialExprFunction[Expr, Z, Unit](
281+
ev.contraNone,
282+
_ => ()
283+
),
284+
)
285+
286+
@ifdef("scalaBinaryVersion:3")
287+
implicit def idUnitUnit:BiEithered[Id, Unit, Unit, Unit] = idSymmetric[Unit]
288+
}
289+
290+
private[typeclass] trait LowPrioBiEithered {
291+
@ifdef("scalaBinaryVersion:3")
292+
implicit def quotedSymmetric[A](implicit quotes:scala.quoted.Quotes):BiEithered[scala.quoted.Expr, A, A, A] = BiEithered.apply(
293+
Predef.identity _,
294+
Predef.identity _,
295+
PartialExprFunction.identity(scala.quoted.Expr(true)),
296+
PartialExprFunction.identity(scala.quoted.Expr(true)),
297+
)
298+
299+
implicit def idSymmetric[A]:BiEithered[Id, A, A, A] = {
300+
BiEithered.apply[Id, A, A, A](
301+
Predef.identity _,
302+
Predef.identity _,
303+
PartialExprFunction.identity[Id, A](true),
304+
PartialExprFunction.identity[Id, A](true),
305+
)
306+
}
145307
}

0 commit comments

Comments
 (0)