Skip to content

Commit c311750

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

File tree

1 file changed

+52
-59
lines changed

1 file changed

+52
-59
lines changed

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

+52-59
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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

@@ -138,23 +139,7 @@ object ContraOptionally extends LowPrioContraOptionally {
138139
implicit def idUnit:ContraOptionally[IdCtx, Id, Unit, Unit] = BiOptionally.idUnit
139140

140141
@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-
}
142+
implicit def contextUnit[Ctx <: scala.reflect.macros.blackbox.Context with Singleton]:BiOptionally[Ctx, Ctx#Expr, Unit, Unit] = BiOptionally.contextUnit
158143

159144
@ifdef("scalaBinaryVersion:3")
160145
implicit def quotedUnit:BiOptionally[scala.quoted.Quotes, scala.quoted.Expr, Unit, Unit] = BiOptionally.quotedUnit
@@ -164,6 +149,9 @@ object ContraOptionally extends LowPrioContraOptionally {
164149
private[typeclass] trait LowPrioContraOptionally {
165150
implicit def idToOption[A]:ContraOptionally[IdCtx, Id, A, Option[A]] = BiOptionally.idToOption
166151

152+
@ifdef("scalaEpochVersion:2")
153+
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
154+
167155
@ifdef("scalaBinaryVersion:3")
168156
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
169157
}
@@ -212,48 +200,12 @@ object BiOptionally extends LowPrioBiOptionally {
212200
)
213201

214202
@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-
}
203+
def contextUnit[Ctx <: scala.reflect.macros.blackbox.Context with Singleton]:BiOptionally[Ctx, Ctx#Expr, Unit, Unit] = BiOptionally.apply(
204+
_ => (),
205+
(_, _) => (),
206+
(_, ctx) => Exprs.forContext[Ctx].constTrue(ctx),
207+
PartialExprFunction.identity[Ctx, Ctx#Expr, Ctx#TypeTag, Unit](using Exprs.forContext)
208+
)
257209

258210
@ifdef("scalaBinaryVersion:3")
259211
implicit def quotedUnit:BiOptionally[scala.quoted.Quotes, scala.quoted.Expr, Unit, Unit] = BiOptionally.apply(
@@ -278,6 +230,47 @@ private[typeclass] trait LowPrioBiOptionally {
278230
)
279231
)
280232

233+
@ifdef("scalaEpochVersion:2")
234+
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] = {
235+
c.Expr[Z](c.universe.Select(qualifier.tree, c.universe.TermName(name)))
236+
}
237+
@ifdef("scalaEpochVersion:2")
238+
private[this] def selectTermNames[Z](c:scala.reflect.macros.blackbox.Context)(root:String, names:String*)(implicit typZ:c.TypeTag[Z]):c.Expr[Z] = {
239+
val rootTree = c.universe.Ident(c.universe.TermName(root))
240+
val namesTree = names.foldLeft[c.universe.Tree](rootTree)({(folding, name) => c.universe.Select(folding, c.universe.TermName(name))})
241+
c.Expr[Z](namesTree)
242+
}
243+
244+
@ifdef("scalaEpochVersion:2")
245+
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(
246+
(ctx:Ctx) => {
247+
@nowarn("msg=never used") implicit val typA2:ctx.TypeTag[A] = typA.asInstanceOf[ctx.TypeTag[A]]
248+
selectTermNames[Option[A]](ctx)("_root_", "scala", "None").asInstanceOf[Ctx#Expr[Option[A]]]
249+
},
250+
(value, ctx) => {
251+
@nowarn("msg=never used") implicit val typA2:ctx.TypeTag[A] = typA.asInstanceOf[ctx.TypeTag[A]]
252+
val value2 = value.asInstanceOf[ctx.Expr[A]]
253+
val rootTree = ctx.universe.Ident(ctx.universe.TermName("_root_"))
254+
val namesTree = List("scala", "Some", "apply").foldLeft[ctx.universe.Tree](rootTree)({(folding, name) => ctx.universe.Select(folding, ctx.universe.TermName(name))})
255+
ctx.Expr[Option[A]](ctx.universe.Apply(namesTree, List(value2.tree))).asInstanceOf[Ctx#Expr[Option[A]]]
256+
},
257+
(value, ctx) => {
258+
val value2 = value.asInstanceOf[ctx.Expr[Option[A]]]
259+
select[Option[A], Boolean](ctx)(value2, "isEmpty").asInstanceOf[Ctx#Expr[Boolean]]
260+
},
261+
PartialExprFunction(
262+
(value, ctx) => {
263+
val value2 = value.asInstanceOf[ctx.Expr[Option[A]]]
264+
select[Option[A], Boolean](ctx)(value2, "nonEmpty").asInstanceOf[Ctx#Expr[Boolean]]
265+
},
266+
(value, ctx) => {
267+
val value2 = value.asInstanceOf[ctx.Expr[Option[A]]]
268+
select[Option[A], A](ctx)(value2, "get").asInstanceOf[Ctx#Expr[A]]
269+
},
270+
)
271+
)
272+
273+
@nowarn("msg=make nowarn used")
281274
@ifdef("scalaBinaryVersion:3")
282275
implicit def quotedToExprOption[A](implicit typ: TypeCreator[A]):BiOptionally[scala.quoted.Quotes, scala.quoted.Expr, scala.quoted.Expr[A], scala.quoted.Expr[Option[A]]] =
283276
OptionallyImpl.quotedToExprOption[A]

0 commit comments

Comments
 (0)