1
1
package name .rayrobdod .stringContextParserCombinator
2
2
package typeclass
3
3
4
+ import com .eed3si9n .ifdef .ifdef
5
+
4
6
/**
5
7
* Describes how to represent an optional value
6
8
*
@@ -113,7 +115,7 @@ private[typeclass] trait LowPrioOptionally {
113
115
* Predefined implicit implementations of ContraOptionally
114
116
* and methods to create new ContraOptionally
115
117
*/
116
- object ContraOptionally extends VersionSpecificContraOptionally with LowPrioContraOptionally {
118
+ object ContraOptionally extends LowPrioContraOptionally {
117
119
/**
118
120
* Constructs an `ContraOptionally` from a set of functions corresponding to each of ContraOptionally's methods
119
121
*/
@@ -129,17 +131,43 @@ object ContraOptionally extends VersionSpecificContraOptionally with LowPrioCont
129
131
}
130
132
131
133
implicit def idUnit : ContraOptionally [Id , Unit , Unit ] = BiOptionally .idUnit
134
+
135
+ @ ifdef(" scalaEpochVersion:2" )
136
+ trait ContraOptionallys [Expr [_], Type [_]] extends LowPrioContraOptionallys [Expr , Type ] {
137
+ implicit def unit : ContraOptionally [Expr , Unit , Unit ]
138
+ }
139
+ @ ifdef(" scalaEpochVersion:2" )
140
+ trait LowPrioContraOptionallys [Expr [_], Type [_]] {
141
+ implicit def toExprOption [A ](implicit typA: Type [A ]): ContraOptionally [Expr , Expr [A ], Expr [Option [A ]]]
142
+ }
143
+
144
+ @ ifdef(" scalaEpochVersion:2" )
145
+ def forContext (c: scala.reflect.macros.blackbox.Context ): ContraOptionallys [c.Expr , c.TypeTag ] = {
146
+ new ContraOptionallys [c.Expr , c.TypeTag ] {
147
+ private [this ] val backing = BiOptionally .forContext(c)
148
+
149
+ override def unit : ContraOptionally [c.Expr , Unit , Unit ] = backing.unit
150
+ override def toExprOption [A ](implicit typA: c.TypeTag [A ]): ContraOptionally [c.Expr , c.Expr [A ], c.Expr [Option [A ]]] = backing.toExprOption[A ]
151
+ }
152
+ }
153
+
154
+ @ ifdef(" scalaBinaryVersion:3" )
155
+ implicit def quotedUnit (implicit quotes : scala.quoted.Quotes ): BiOptionally [scala.quoted.Expr , Unit , Unit ] = BiOptionally .quotedUnit
156
+
132
157
}
133
158
134
- private [typeclass] trait LowPrioContraOptionally extends VersionSpecificLowPrioContraOptionally {
159
+ private [typeclass] trait LowPrioContraOptionally {
135
160
implicit def idToOption [A ]: ContraOptionally [Id , A , Option [A ]] = BiOptionally .idToOption
161
+
162
+ @ ifdef(" scalaBinaryVersion:3" )
163
+ implicit def quotedToExprOption [A ](implicit quotes : scala.quoted.Quotes , typA : scala.quoted.Type [A ]): BiOptionally [scala.quoted.Expr , scala.quoted.Expr [A ], scala.quoted.Expr [Option [A ]]] = BiOptionally .quotedToExprOption
136
164
}
137
165
138
166
/**
139
167
* Predefined implicit implementations of BiOptionally
140
168
* and methods to create new BiOptionally
141
169
*/
142
- object BiOptionally extends VersionSpecificBiOptionally with LowPrioBiOptionally {
170
+ object BiOptionally extends LowPrioBiOptionally {
143
171
/**
144
172
* Constructs an `BiOptionally` from a set of functions corresponding to each of BiOptionally's methods
145
173
*/
@@ -164,9 +192,62 @@ object BiOptionally extends VersionSpecificBiOptionally with LowPrioBiOptionally
164
192
_ => true ,
165
193
PartialExprFunction .identity[Id , Unit ](true )
166
194
)
195
+
196
+ @ ifdef(" scalaEpochVersion:2" )
197
+ trait BiOptionallys [Expr [_], Type [_]] extends LowPrioBiOptionallys [Expr , Type ] {
198
+ implicit def unit : BiOptionally [Expr , Unit , Unit ]
199
+ }
200
+ @ ifdef(" scalaEpochVersion:2" )
201
+ trait LowPrioBiOptionallys [Expr [_], Type [_]] {
202
+ implicit def toExprOption [A ](implicit typA: Type [A ]): BiOptionally [Expr , Expr [A ], Expr [Option [A ]]]
203
+ }
204
+
205
+ @ ifdef(" scalaEpochVersion:2" )
206
+ def forContext (c: scala.reflect.macros.blackbox.Context ): BiOptionallys [c.Expr , c.TypeTag ] = {
207
+ new BiOptionallys [c.Expr , c.TypeTag ] {
208
+ private [this ] val exprTrue = c.Expr [Boolean ](c.universe.Literal (c.universe.Constant (true )))
209
+ private [this ] def select [A , Z ](qualifier: c.Expr [A ], name: String )(implicit typZ: c.TypeTag [Z ]): c.Expr [Z ] = {
210
+ c.Expr [Z ](c.universe.Select (qualifier.tree, c.universe.TermName (name)))
211
+ }
212
+ private [this ] def selectTermNames [Z ](root: String , names: String * )(implicit typZ: c.TypeTag [Z ]): c.Expr [Z ] = {
213
+ val rootTree = c.universe.Ident (c.universe.TermName (root))
214
+ val namesTree = names.foldLeft[c.universe.Tree ](rootTree)({(folding, name) => c.universe.Select (folding, c.universe.TermName (name))})
215
+ c.Expr [Z ](namesTree)
216
+ }
217
+
218
+ override def unit : BiOptionally [c.Expr , Unit , Unit ] = BiOptionally .apply(
219
+ (),
220
+ _ => (),
221
+ _ => exprTrue,
222
+ PartialExprFunction .identity(exprTrue)
223
+ )
224
+
225
+ override def toExprOption [A ](implicit typA: c.TypeTag [A ]): BiOptionally [c.Expr , c.Expr [A ], c.Expr [Option [A ]]] = BiOptionally .apply(
226
+ selectTermNames[Option [A ]](" _root_" , " scala" , " None" ),
227
+ value => {
228
+ val rootTree = c.universe.Ident (c.universe.TermName (" _root_" ))
229
+ val namesTree = List (" scala" , " Some" , " apply" ).foldLeft[c.universe.Tree ](rootTree)({(folding, name) => c.universe.Select (folding, c.universe.TermName (name))})
230
+ c.Expr [Option [A ]](c.universe.Apply (namesTree, List (value.tree)))
231
+ },
232
+ value => select[Option [A ], Boolean ](value, " isEmpty" ),
233
+ PartialExprFunction (
234
+ value => select[Option [A ], Boolean ](value, " nonEmpty" ),
235
+ value => select[Option [A ], A ](value, " get" )
236
+ )
237
+ )
238
+ }
239
+ }
240
+
241
+ @ ifdef(" scalaBinaryVersion:3" )
242
+ implicit def quotedUnit (implicit quotes : scala.quoted.Quotes ): BiOptionally [scala.quoted.Expr , Unit , Unit ] = BiOptionally .apply(
243
+ (),
244
+ _ => (),
245
+ _ => scala.quoted.Expr (true ),
246
+ PartialExprFunction .identity(scala.quoted.Expr (true )),
247
+ )
167
248
}
168
249
169
- private [typeclass] trait LowPrioBiOptionally extends VersionSpecificLowPrioBiOptionally {
250
+ private [typeclass] trait LowPrioBiOptionally {
170
251
implicit def idToOption [A ]: BiOptionally [Id , A , Option [A ]] = BiOptionally .apply[Id , A , Option [A ]](
171
252
None ,
172
253
Some .apply _,
@@ -176,4 +257,8 @@ private[typeclass] trait LowPrioBiOptionally extends VersionSpecificLowPrioBiOpt
176
257
_.get
177
258
)
178
259
)
260
+
261
+ @ ifdef(" scalaBinaryVersion:3" )
262
+ implicit def quotedToExprOption [A ](implicit quotes : scala.quoted.Quotes , typ : scala.quoted.Type [A ]): BiOptionally [scala.quoted.Expr , scala.quoted.Expr [A ], scala.quoted.Expr [Option [A ]]] =
263
+ OptionallyImpl .quotedToExprOption[A ]
179
264
}
0 commit comments