File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed
Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 1+ package a
2+
3+ import scala .quoted ._
4+ import scala .concurrent ._
5+
6+ object M {
7+
8+ inline def resolveInMacros [F [_],T ](f : Future [T ]): Conversion [Future [T ],F [T ]] =
9+ $ { resolveInMacrosImpl[F ,T ](' f ) }
10+
11+ def resolveInMacrosImpl [F [_]: Type ,T : Type ](f: Expr [Future [T ]])(using qctx: Quotes ): Expr [
12+ Conversion [Future [T ],F [T ]]]= {
13+ import quotes .reflect ._
14+ val conversion = TypeIdent (Symbol .classSymbol(" scala.Conversion" )).tpe
15+ val inFuture = f.asTerm.tpe.widen
16+ val tType = TypeRepr .of[T ]
17+ val fType = TypeRepr .of[F ]
18+ val inCB = fType.appliedTo(tType).simplified
19+ val taConversion = conversion.appliedTo(List (inFuture, inCB))
20+ Implicits .search(taConversion) match
21+ case implSuccess : ImplicitSearchSuccess =>
22+ implSuccess.tree.asExpr.asInstanceOf [Expr [Conversion [Future [T ],F [T ]]]]
23+ case implFailure : ImplicitSearchFailure =>
24+ println(s " searchFailure: ${implFailure.explanation}" )
25+ throw new RuntimeException (" implicit search failed" )
26+ }
27+
28+ }
Original file line number Diff line number Diff line change 1+ package a
2+
3+ import scala .language .implicitConversions
4+ import scala .concurrent ._
5+
6+ trait CB [T ]
7+
8+ given myConversion [T ]: Conversion [Future [T ],CB [T ]] = (ft => ??? )
9+
10+ object O {
11+
12+ def main (argvs : Array [String ]): Unit = {
13+ val p = Promise [Int ]()
14+ // val cbp = summon[Conversion[Future[Int],CB[Int]]] //works
15+ val cbp = M .resolveInMacros[CB ,Int ](p.future)
16+ val x = cbp(p.future)
17+ }
18+
19+ }
You can’t perform that action at this time.
0 commit comments