Skip to content

Commit 4b5e4b6

Browse files
committed
[base] Flatten out [Bi|Contra]Eithered.forContext
1 parent 1200296 commit 4b5e4b6

File tree

4 files changed

+22
-46
lines changed

4 files changed

+22
-46
lines changed

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

+15-41
Original file line numberDiff line numberDiff line change
@@ -192,24 +192,7 @@ object ContraEithered extends LowPrioContraEithered {
192192
}
193193

194194
@ifdef("scalaEpochVersion:2")
195-
trait ContraEithereds[Ctx, Expr[+_]] extends LowPrioContraEithereds[Ctx, Expr] {
196-
implicit def unitUnit:ContraEithered[Ctx, Expr, Unit, Unit, Unit]
197-
}
198-
@ifdef("scalaEpochVersion:2")
199-
private[typeclass]
200-
trait LowPrioContraEithereds[Ctx, Expr[+_]] {
201-
implicit def symmetric[A]:ContraEithered[Ctx, Expr, A, A, A]
202-
}
203-
204-
@ifdef("scalaEpochVersion:2")
205-
def forContext(c:scala.reflect.macros.blackbox.Context):ContraEithereds[c.type, c.Expr] = {
206-
val backing = BiEithered.forContext(c)
207-
208-
new ContraEithereds[c.type, c.Expr] {
209-
implicit override def unitUnit:ContraEithered[c.type, c.Expr, Unit, Unit, Unit] = backing.unitUnit
210-
implicit override def symmetric[A]:ContraEithered[c.type, c.Expr, A, A, A] = backing.symmetric[A]
211-
}
212-
}
195+
implicit def contextUnitUnit[Ctx <: scala.reflect.macros.blackbox.Context with Singleton, A]:ContraEithered[Ctx, Ctx#Expr, Unit, Unit, Unit] = BiEithered.contextUnitUnit[Ctx]
213196

214197
@ifdef("scalaBinaryVersion:3")
215198
implicit def quotedUnitUnit:ContraEithered[scala.quoted.Quotes, scala.quoted.Expr, Unit, Unit, Unit] = quotedSymmetric[Unit]
@@ -221,6 +204,9 @@ private[typeclass] trait LowPrioContraEithered {
221204
@ifdef("scalaBinaryVersion:3")
222205
implicit def quotedSymmetric[A]:ContraEithered[scala.quoted.Quotes, scala.quoted.Expr, A, A, A] = BiEithered.quotedSymmetric
223206

207+
@ifdef("scalaEpochVersion:2")
208+
implicit def contextSymmetric[Ctx <: scala.reflect.macros.blackbox.Context with Singleton, A]:ContraEithered[Ctx, Ctx#Expr, A, A, A] = BiEithered.contextSymmetric[Ctx, A]
209+
224210
implicit def idSymmetric[A]:ContraEithered[IdCtx, Id, A, A, A] = BiEithered.idSymmetric
225211
}
226212

@@ -249,29 +235,7 @@ object BiEithered extends LowPrioBiEithered {
249235
}
250236

251237
@ifdef("scalaEpochVersion:2")
252-
trait BiEithereds[Ctx, Expr[+_]] extends LowPrioBiEithereds[Ctx, Expr] {
253-
implicit def unitUnit:BiEithered[Ctx, Expr, Unit, Unit, Unit]
254-
}
255-
@ifdef("scalaEpochVersion:2")
256-
private[typeclass]
257-
trait LowPrioBiEithereds[Ctx, Expr[+_]] {
258-
implicit def symmetric[A]:BiEithered[Ctx, Expr, A, A, A]
259-
}
260-
@ifdef("scalaEpochVersion:2")
261-
def forContext(c:scala.reflect.macros.blackbox.Context):BiEithereds[c.type, c.Expr] = {
262-
new BiEithereds[c.type, c.Expr] {
263-
override def unitUnit:BiEithered[c.type, c.Expr, Unit, Unit, Unit] = this.symmetric[Unit]
264-
265-
implicit override def symmetric[A]:BiEithered[c.type, c.Expr, A, A, A] = {
266-
BiEithered.apply[c.type, c.Expr, A, A, A](
267-
(value, _) => value,
268-
(value, _) => value,
269-
PartialExprFunction.identity[c.type, c.Expr, c.TypeTag, A](using typeclass.Exprs.forContext[c.type]),
270-
PartialExprFunction.identity[c.type, c.Expr, c.TypeTag, A](using typeclass.Exprs.forContext[c.type]),
271-
)
272-
}
273-
}
274-
}
238+
implicit def contextUnitUnit[Ctx <: scala.reflect.macros.blackbox.Context with Singleton]:BiEithered[Ctx, Ctx#Expr, Unit, Unit, Unit] = this.contextSymmetric[Ctx, Unit]
275239

276240
@ifdef("scalaBinaryVersion:3")
277241
implicit def quotedUnitUnit:BiEithered[scala.quoted.Quotes, scala.quoted.Expr, Unit, Unit, Unit] = quotedSymmetric[Unit]
@@ -314,6 +278,16 @@ private[typeclass] trait LowPrioBiEithered {
314278
)
315279
}
316280

281+
@ifdef("scalaEpochVersion:2")
282+
implicit def contextSymmetric[Ctx <: scala.reflect.macros.blackbox.Context with Singleton, A]:BiEithered[Ctx, Ctx#Expr, A, A, A] = {
283+
BiEithered.apply[Ctx, Ctx#Expr, A, A, A](
284+
(value, _) => value,
285+
(value, _) => value,
286+
PartialExprFunction.identity[Ctx, Ctx#Expr, Ctx#TypeTag, A](using typeclass.Exprs.forContext[Ctx]),
287+
PartialExprFunction.identity[Ctx, Ctx#Expr, Ctx#TypeTag, A](using typeclass.Exprs.forContext[Ctx]),
288+
)
289+
}
290+
317291
implicit def idSymmetric[A]:BiEithered[IdCtx, Id, A, A, A] = {
318292
BiEithered.apply[IdCtx, Id, A, A, A](
319293
(value, _) => value,

CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
* Flatten Interpolator, Extractor, Parser and companions' inheritance hierarchy
1111
* Include pages for `Interpolator.LiftedInterpolator` inner classes in docs
1212
* Use `ifdef` annotation instead of inheritance for scala-version-specific methods
13-
* Add `Ctx` type parameters and `ctx:Ctx` parameters to a lot of classes, methods, and function paramters
13+
* Add `Ctx` type parameters and `ctx:Ctx` parameters to a lot of classes, methods, and function parameters
1414
* On scala-3, add overloads to some parsers/extractors/interpolators methods such that one has a function parameter with an explicit Ctx parameter and the other has a function parameter with an implicit Ctx parameter
1515
* Makes this library `-Xcheck-macros`-safe
1616
* `scpc.IdCtx` created as the `Ctx` for the identity context
1717
* Removed `quoted.Quotes` or `blackbox.Context` using parameters from leaf parser methods
18+
* Flatten out typeclass `forContext` methods, putting respective methods that do not require a `Context` instance to create directly on the companion object
1819
* Create `TypeCreator` class; Change `Quotes`-context associated `Type[_]` from `quotes.Type` to `scpc.TypeCreator`
1920

2021
## [0.1.1](https://github.com/rayrobdod/string-context-parser-combinator/compare/0.1.0...0.1.1) – 2025-02-04

JsonParser/src/main/scala-2/MacroImpl.scala

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ final class MacroImpl(val c:Context {type PrefixType = JsonStringContext}) {
2929
import myInterpolators.lifted
3030
private[this] val myRepeateds = typeclass.Repeated.forContext(c)
3131
import myRepeateds._
32-
private[this] val myBiEithered = typeclass.BiEithered.forContext(c)
33-
import myBiEithered._
3432
private[this] val myBiRepeateds = typeclass.BiRepeated.forContext(c)
3533
import myBiRepeateds._
3634

35+
// 2.13 can find `contextSymmetric` without the local variable. 2.12 cannot
36+
private[this] implicit def mySymmetricBiEithered[A]: typeclass.BiEithered[c.type, c.Expr, A, A, A] = typeclass.BiEithered.contextSymmetric[c.type, A]
37+
3738
private[this] def charFlatCollect[A](pf: PartialFunction[Char, Interpolator[A]]):Interpolator[A] = {
3839
charWhere(pf.isDefinedAt)
3940
.flatMap((x, _) => pf.apply(x))

TimeParser/src/main/scala-2/package.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ package datetime {
1111

1212
private[this] val leafParsers = Parser.contextParsers(c)
1313
import leafParsers._
14-
private[this] val bieithereds = typeclass.BiEithered.forContext(c)
15-
import bieithereds._
14+
// 2.13 can find `contextSymmetric` without the local variable. 2.12 cannot
15+
private[this] implicit def mySymmetricBiEithered[A]: typeclass.BiEithered[c.type, c.Expr, A, A, A] = typeclass.BiEithered.contextSymmetric[c.type, A]
1616
private[this] val timeLiftables = TimeLiftables(c)
1717
import timeLiftables._
1818
private[this] val timeUnliftables = TimeUnliftables(c)

0 commit comments

Comments
 (0)