Skip to content

Commit f395cb8

Browse files
committed
Merge branch 'flatten-typeclass-companion'
2 parents 1200296 + 54b6b00 commit f395cb8

File tree

9 files changed

+364
-417
lines changed

9 files changed

+364
-417
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,13 @@ package stringContextParserCombinator {
8585
trait LiftFunction[U <: Context with Singleton, -CC[_], +Z] {def apply[A](lifter:U#Expr[CC[A]], elem:U#Expr[A]):Z}
8686
/** A context avaliable for the identity context. Essentially a `Unit`, but with an implicit value */
8787
final class IdCtx()
88+
89+
/** `type Ctx <: Context with Singleton` and `val ctx: Ctx` apparently does not imply `Ctx =:= ctx.type` */
90+
private[stringContextParserCombinator]
91+
final class BindSingletonContexts[Ctx1 <: Context with Singleton, Ctx2 <: Context with Singleton] {
92+
implicit def ttToLeft[A](a: Ctx1#TypeTag[A]): Ctx2#TypeTag[A] = a.asInstanceOf[Ctx2#TypeTag[A]]
93+
implicit def ttToRight[A](a: Ctx2#TypeTag[A]): Ctx1#TypeTag[A] = a.asInstanceOf[Ctx1#TypeTag[A]]
94+
implicit def exprToLeft[A](a: Ctx1#Expr[A]): Ctx2#Expr[A] = a.asInstanceOf[Ctx2#Expr[A]]
95+
implicit def exprToRight[A](a: Ctx2#Expr[A]): Ctx1#Expr[A] = a.asInstanceOf[Ctx1#Expr[A]]
96+
}
8897
}

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

+17-54
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,8 @@ object Eithered extends LowPrioEithered {
102102
* @version 0.1.1
103103
*/
104104
@ifdef("scalaEpochVersion:2")
105-
trait Eithereds[Ctx, Expr[+_]] {
106-
def splicePiece[A]: Eithered[Ctx, Expr[A], Expr[Iterable[A]], Repeated.SplicePiece[Expr, A]]
107-
}
108-
/**
109-
* @version 0.1.1
110-
*/
111-
@ifdef("scalaEpochVersion:2")
112-
def forContext(c:scala.reflect.macros.blackbox.Context):Eithereds[c.type, c.Expr] = {
113-
new Eithereds[c.type, c.Expr] {
114-
def splicePiece[A]: Eithered[c.type, c.Expr[A], c.Expr[Iterable[A]], Repeated.SplicePiece[c.Expr, A]] =
115-
Eithered((value, _) => new Repeated.SplicePiece.One(value), (value, _) => new Repeated.SplicePiece.Many(value))
116-
}
117-
}
105+
def contextSplicePiece[Ctx <: scala.reflect.macros.blackbox.Context with Singleton, A]: Eithered[Ctx, Ctx#Expr[A], Ctx#Expr[Iterable[A]], Repeated.SplicePiece[Ctx#Expr, A]] =
106+
Eithered((value, _) => new Repeated.SplicePiece.One(value), (value, _) => new Repeated.SplicePiece.Many(value))
118107

119108
/**
120109
* @version 0.1.1
@@ -192,24 +181,7 @@ object ContraEithered extends LowPrioContraEithered {
192181
}
193182

194183
@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-
}
184+
implicit def contextUnitUnit[Ctx <: scala.reflect.macros.blackbox.Context with Singleton, A]:ContraEithered[Ctx, Ctx#Expr, Unit, Unit, Unit] = BiEithered.contextUnitUnit[Ctx]
213185

214186
@ifdef("scalaBinaryVersion:3")
215187
implicit def quotedUnitUnit:ContraEithered[scala.quoted.Quotes, scala.quoted.Expr, Unit, Unit, Unit] = quotedSymmetric[Unit]
@@ -221,6 +193,9 @@ private[typeclass] trait LowPrioContraEithered {
221193
@ifdef("scalaBinaryVersion:3")
222194
implicit def quotedSymmetric[A]:ContraEithered[scala.quoted.Quotes, scala.quoted.Expr, A, A, A] = BiEithered.quotedSymmetric
223195

196+
@ifdef("scalaEpochVersion:2")
197+
implicit def contextSymmetric[Ctx <: scala.reflect.macros.blackbox.Context with Singleton, A]:ContraEithered[Ctx, Ctx#Expr, A, A, A] = BiEithered.contextSymmetric[Ctx, A]
198+
224199
implicit def idSymmetric[A]:ContraEithered[IdCtx, Id, A, A, A] = BiEithered.idSymmetric
225200
}
226201

@@ -249,29 +224,7 @@ object BiEithered extends LowPrioBiEithered {
249224
}
250225

251226
@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-
}
227+
implicit def contextUnitUnit[Ctx <: scala.reflect.macros.blackbox.Context with Singleton]:BiEithered[Ctx, Ctx#Expr, Unit, Unit, Unit] = this.contextSymmetric[Ctx, Unit]
275228

276229
@ifdef("scalaBinaryVersion:3")
277230
implicit def quotedUnitUnit:BiEithered[scala.quoted.Quotes, scala.quoted.Expr, Unit, Unit, Unit] = quotedSymmetric[Unit]
@@ -314,6 +267,16 @@ private[typeclass] trait LowPrioBiEithered {
314267
)
315268
}
316269

270+
@ifdef("scalaEpochVersion:2")
271+
implicit def contextSymmetric[Ctx <: scala.reflect.macros.blackbox.Context with Singleton, A]:BiEithered[Ctx, Ctx#Expr, A, A, A] = {
272+
BiEithered.apply[Ctx, Ctx#Expr, A, A, A](
273+
(value, _) => value,
274+
(value, _) => value,
275+
PartialExprFunction.identity[Ctx, Ctx#Expr, Ctx#TypeTag, A](using typeclass.Exprs.forContext[Ctx]),
276+
PartialExprFunction.identity[Ctx, Ctx#Expr, Ctx#TypeTag, A](using typeclass.Exprs.forContext[Ctx]),
277+
)
278+
}
279+
317280
implicit def idSymmetric[A]:BiEithered[IdCtx, Id, A, A, A] = {
318281
BiEithered.apply[IdCtx, Id, A, A, A](
319282
(value, _) => value,

Base/src/main/scala/typeclass/Exprs.scala

+15-9
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,28 @@ object Exprs {
4646
ctx.Expr[Boolean](ctx.universe.Liftable.liftBoolean(b))
4747
}
4848

49-
override def andBooleans(left2: Ctx#Expr[Boolean], rightFn: () => Ctx#Expr[Boolean])(implicit ctx: Ctx): Ctx#Expr[Boolean] = {
49+
override def andBooleans(left: Ctx#Expr[Boolean], rightFn: () => Ctx#Expr[Boolean])(implicit ctx: Ctx): Ctx#Expr[Boolean] = {
50+
val myBindSingletonContexts = new BindSingletonContexts[Ctx, ctx.type]
51+
import myBindSingletonContexts._
52+
5053
val B = ctx.universe.Unliftable.unliftBoolean
51-
val left = left2.asInstanceOf[ctx.Expr[Boolean]]
52-
val right = rightFn().asInstanceOf[ctx.Expr[Boolean]]
53-
(left.tree, right.tree) match {
54-
case (B(true), _) => right
54+
val left2: ctx.Expr[Boolean] = left
55+
val right2: ctx.Expr[Boolean] = rightFn()
56+
(left2.tree, right2.tree) match {
57+
case (B(true), _) => right2
5558
case (B(false), _) => this.constFalse
56-
case (_, B(true)) => left
59+
case (_, B(true)) => left2
5760
case (_, B(false)) => this.constFalse
58-
case (_, _) => selectApply[Boolean](ctx)(left, "$amp$amp", right)
61+
case (_, _) => selectApply[Boolean](ctx)(left2, "$amp$amp", right2)
5962
}
6063
}
6164

6265
override def isEqualTo[A](left: Ctx#Expr[A], right: Ctx#Expr[A])(implicit ctx: Ctx, typA: Ctx#TypeTag[A]): Ctx#Expr[Boolean] = {
63-
val left2 = left.asInstanceOf[ctx.Expr[A]]
64-
val right2 = right.asInstanceOf[ctx.Expr[A]]
66+
val myBindSingletonContexts = new BindSingletonContexts[Ctx, ctx.type]
67+
import myBindSingletonContexts._
68+
69+
val left2: ctx.Expr[A] = left
70+
val right2: ctx.Expr[A] = right
6571
selectApply[Boolean](ctx)(left2, "$eq$eq", right2)
6672
}
6773
}

Base/src/main/scala/typeclass/Optionally.scala

+64-59
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package name.rayrobdod.stringContextParserCombinator
22
package typeclass
33

4+
import scala.annotation.nowarn
45
import scala.reflect.ClassTag
56
import com.eed3si9n.ifdef.ifdef
67

8+
@nowarn("msg=make nowarn used")
9+
private[typeclass] final class Optionally_MakeNowarnUsed
10+
711
/**
812
* Describes how to represent an optional value
913
*
@@ -138,23 +142,7 @@ object ContraOptionally extends LowPrioContraOptionally {
138142
implicit def idUnit:ContraOptionally[IdCtx, Id, Unit, Unit] = BiOptionally.idUnit
139143

140144
@ifdef("scalaEpochVersion:2")
141-
trait ContraOptionallys[Ctx, Expr[+_], Type[_]] extends LowPrioContraOptionallys[Ctx, Expr, Type] {
142-
implicit def unit:ContraOptionally[Ctx, Expr, Unit, Unit]
143-
}
144-
@ifdef("scalaEpochVersion:2")
145-
trait LowPrioContraOptionallys[Ctx, Expr[+_], Type[_]] {
146-
implicit def toExprOption[A](implicit typA:Type[A]):ContraOptionally[Ctx, Expr, Expr[A], Expr[Option[A]]]
147-
}
148-
149-
@ifdef("scalaEpochVersion:2")
150-
def forContext(c:scala.reflect.macros.blackbox.Context):ContraOptionallys[c.type, c.Expr, c.TypeTag] = {
151-
new ContraOptionallys[c.type, c.Expr, c.TypeTag] {
152-
private[this] val backing = BiOptionally.forContext(c)
153-
154-
override def unit:ContraOptionally[c.type, c.Expr, Unit, Unit] = backing.unit
155-
override def toExprOption[A](implicit typA:c.TypeTag[A]):ContraOptionally[c.type, c.Expr, c.Expr[A], c.Expr[Option[A]]] = backing.toExprOption[A]
156-
}
157-
}
145+
implicit def contextUnit[Ctx <: scala.reflect.macros.blackbox.Context with Singleton]:BiOptionally[Ctx, Ctx#Expr, Unit, Unit] = BiOptionally.contextUnit
158146

159147
@ifdef("scalaBinaryVersion:3")
160148
implicit def quotedUnit:BiOptionally[scala.quoted.Quotes, scala.quoted.Expr, Unit, Unit] = BiOptionally.quotedUnit
@@ -164,6 +152,9 @@ object ContraOptionally extends LowPrioContraOptionally {
164152
private[typeclass] trait LowPrioContraOptionally {
165153
implicit def idToOption[A]:ContraOptionally[IdCtx, Id, A, Option[A]] = BiOptionally.idToOption
166154

155+
@ifdef("scalaEpochVersion:2")
156+
implicit def contextToExprOption[Ctx <: scala.reflect.macros.blackbox.Context with Singleton, A](implicit typA:Ctx#TypeTag[A]):BiOptionally[Ctx, Ctx#Expr, Ctx#Expr[A], Ctx#Expr[Option[A]]] = BiOptionally.contextToExprOption
157+
167158
@ifdef("scalaBinaryVersion:3")
168159
implicit def quotedToExprOption[A](implicit typA: TypeCreator[A]):BiOptionally[scala.quoted.Quotes, scala.quoted.Expr, scala.quoted.Expr[A], scala.quoted.Expr[Option[A]]] = BiOptionally.quotedToExprOption
169160
}
@@ -212,48 +203,12 @@ object BiOptionally extends LowPrioBiOptionally {
212203
)
213204

214205
@ifdef("scalaEpochVersion:2")
215-
trait BiOptionallys[Ctx, Expr[+_], Type[_]] extends LowPrioBiOptionallys[Ctx, Expr, Type] {
216-
implicit def unit:BiOptionally[Ctx, Expr, Unit, Unit]
217-
}
218-
@ifdef("scalaEpochVersion:2")
219-
trait LowPrioBiOptionallys[Ctx, Expr[+_], Type[_]] {
220-
implicit def toExprOption[A](implicit typA:Type[A]):BiOptionally[Ctx, Expr, Expr[A], Expr[Option[A]]]
221-
}
222-
223-
@ifdef("scalaEpochVersion:2")
224-
def forContext(c:scala.reflect.macros.blackbox.Context):BiOptionallys[c.type, c.Expr, c.TypeTag] = {
225-
new BiOptionallys[c.type, c.Expr, c.TypeTag] {
226-
private[this] def select[A, Z](qualifier:c.Expr[A], name:String)(implicit typZ:c.TypeTag[Z]):c.Expr[Z] = {
227-
c.Expr[Z](c.universe.Select(qualifier.tree, c.universe.TermName(name)))
228-
}
229-
private[this] def selectTermNames[Z](root:String, names:String*)(implicit typZ:c.TypeTag[Z]):c.Expr[Z] = {
230-
val rootTree = c.universe.Ident(c.universe.TermName(root))
231-
val namesTree = names.foldLeft[c.universe.Tree](rootTree)({(folding, name) => c.universe.Select(folding, c.universe.TermName(name))})
232-
c.Expr[Z](namesTree)
233-
}
234-
235-
override def unit:BiOptionally[c.type, c.Expr, Unit, Unit] = BiOptionally.apply(
236-
_ => (),
237-
(_, _) => (),
238-
(_, ctx) => Exprs.forContext[c.type].constTrue(ctx),
239-
PartialExprFunction.identity[c.type, c.Expr, c.TypeTag, Unit](using Exprs.forContext)
240-
)
241-
242-
override def toExprOption[A](implicit typA:c.TypeTag[A]):BiOptionally[c.type, c.Expr, c.Expr[A], c.Expr[Option[A]]] = BiOptionally.apply(
243-
_ => selectTermNames[Option[A]]("_root_", "scala", "None"),
244-
(value, _) => {
245-
val rootTree = c.universe.Ident(c.universe.TermName("_root_"))
246-
val namesTree = List("scala", "Some", "apply").foldLeft[c.universe.Tree](rootTree)({(folding, name) => c.universe.Select(folding, c.universe.TermName(name))})
247-
c.Expr[Option[A]](c.universe.Apply(namesTree, List(value.tree)))
248-
},
249-
(value, _) => select[Option[A], Boolean](value, "isEmpty"),
250-
PartialExprFunction(
251-
(value, _) => select[Option[A], Boolean](value, "nonEmpty"),
252-
(value, _) => select[Option[A], A](value, "get")
253-
)
254-
)
255-
}
256-
}
206+
def contextUnit[Ctx <: scala.reflect.macros.blackbox.Context with Singleton]:BiOptionally[Ctx, Ctx#Expr, Unit, Unit] = BiOptionally.apply(
207+
_ => (),
208+
(_, _) => (),
209+
(_, ctx) => Exprs.forContext[Ctx].constTrue(ctx),
210+
PartialExprFunction.identity[Ctx, Ctx#Expr, Ctx#TypeTag, Unit](using Exprs.forContext)
211+
)
257212

258213
@ifdef("scalaBinaryVersion:3")
259214
implicit def quotedUnit:BiOptionally[scala.quoted.Quotes, scala.quoted.Expr, Unit, Unit] = BiOptionally.apply(
@@ -278,6 +233,56 @@ private[typeclass] trait LowPrioBiOptionally {
278233
)
279234
)
280235

236+
@ifdef("scalaEpochVersion:2")
237+
private[this] def select[A, Z](c:scala.reflect.macros.blackbox.Context)(qualifier:c.Expr[A], name:String)(implicit typZ:c.TypeTag[Z]):c.Expr[Z] = {
238+
c.Expr[Z](c.universe.Select(qualifier.tree, c.universe.TermName(name)))
239+
}
240+
@ifdef("scalaEpochVersion:2")
241+
private[this] def selectTermNames[Z](c:scala.reflect.macros.blackbox.Context)(root:String, names:String*)(implicit typZ:c.TypeTag[Z]):c.Expr[Z] = {
242+
val rootTree = c.universe.Ident(c.universe.TermName(root))
243+
val namesTree = names.foldLeft[c.universe.Tree](rootTree)({(folding, name) => c.universe.Select(folding, c.universe.TermName(name))})
244+
c.Expr[Z](namesTree)
245+
}
246+
247+
@ifdef("scalaEpochVersion:2")
248+
def contextToExprOption[Ctx <: scala.reflect.macros.blackbox.Context with Singleton, A](implicit typA:Ctx#TypeTag[A]):BiOptionally[Ctx, Ctx#Expr, Ctx#Expr[A], Ctx#Expr[Option[A]]] = BiOptionally.apply(
249+
(ctx:Ctx) => {
250+
val myBindSingletonContexts = new BindSingletonContexts[Ctx, ctx.type]
251+
import myBindSingletonContexts._
252+
@nowarn("msg=never used") implicit val typA2:ctx.TypeTag[A] = typA
253+
selectTermNames[Option[A]](ctx)("_root_", "scala", "None"): Ctx#Expr[Option[A]]
254+
},
255+
(value, ctx) => {
256+
val myBindSingletonContexts = new BindSingletonContexts[Ctx, ctx.type]
257+
import myBindSingletonContexts._
258+
@nowarn("msg=never used") implicit val typA2:ctx.TypeTag[A] = typA
259+
val value2: ctx.Expr[A] = value
260+
val rootTree = ctx.universe.Ident(ctx.universe.TermName("_root_"))
261+
val namesTree = List("scala", "Some", "apply").foldLeft[ctx.universe.Tree](rootTree)({(folding, name) => ctx.universe.Select(folding, ctx.universe.TermName(name))})
262+
ctx.Expr[Option[A]](ctx.universe.Apply(namesTree, List(value2.tree))): Ctx#Expr[Option[A]]
263+
},
264+
(value, ctx) => {
265+
val myBindSingletonContexts = new BindSingletonContexts[Ctx, ctx.type]
266+
import myBindSingletonContexts._
267+
val value2 = value: ctx.Expr[Option[A]]
268+
select[Option[A], Boolean](ctx)(value2, "isEmpty"): Ctx#Expr[Boolean]
269+
},
270+
PartialExprFunction(
271+
(value, ctx) => {
272+
val myBindSingletonContexts = new BindSingletonContexts[Ctx, ctx.type]
273+
import myBindSingletonContexts._
274+
val value2 = value: ctx.Expr[Option[A]]
275+
select[Option[A], Boolean](ctx)(value2, "nonEmpty"): Ctx#Expr[Boolean]
276+
},
277+
(value, ctx) => {
278+
val myBindSingletonContexts = new BindSingletonContexts[Ctx, ctx.type]
279+
import myBindSingletonContexts._
280+
val value2 = value: ctx.Expr[Option[A]]
281+
select[Option[A], A](ctx)(value2, "get"): Ctx#Expr[A]
282+
},
283+
)
284+
)
285+
281286
@ifdef("scalaBinaryVersion:3")
282287
implicit def quotedToExprOption[A](implicit typ: TypeCreator[A]):BiOptionally[scala.quoted.Quotes, scala.quoted.Expr, scala.quoted.Expr[A], scala.quoted.Expr[Option[A]]] =
283288
OptionallyImpl.quotedToExprOption[A]

0 commit comments

Comments
 (0)