-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix divergency implicit errors in scala 2.12
- Loading branch information
Showing
12 changed files
with
233 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package make | ||
|
||
import make.internal.DepMacro | ||
|
||
/** | ||
* This class is used to workaround wrong `divergency implicit error` on scala 2.12 | ||
* Macros uses similar trick as `shapeless.Lazy` but do not allows recursive references | ||
*/ | ||
case class Dep[F[_], A](value: Make[F, A]) | ||
|
||
object Dep { | ||
|
||
implicit def materialize[F[_], A]: Dep[F, A] = | ||
macro DepMacro.materialize[F, A] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package make.internal | ||
|
||
import scala.reflect.macros.whitebox | ||
import scala.collection.mutable | ||
import make.Dep | ||
import make.Make | ||
|
||
class DepMacro(val c: whitebox.Context) { | ||
|
||
import c.universe._ | ||
|
||
def materialize[F[_], A](implicit | ||
ftpe: WeakTypeTag[F[X] forSome { type X }], | ||
atpe: WeakTypeTag[A] | ||
): c.Expr[Dep[F, A]] = { | ||
val state = getOrCreateState | ||
state.infer[F](ftpe.tpe, atpe.tpe) match { | ||
case EmptyTree => | ||
c.abort(c.enclosingPosition, "failed") | ||
case tree => | ||
c.Expr[Dep[F, A]](q"_root_.make.Dep[${ftpe.tpe}, ${atpe.tpe}]($tree)") | ||
} | ||
} | ||
|
||
private def getOrCreateState: State = { | ||
val existing = | ||
c.openMacros.find(c => c.internal.attachments(c.macroApplication).contains[State]) | ||
.flatMap(c => c.internal.attachments(c.macroApplication).get[State]) | ||
existing match { | ||
case None => | ||
val st = new State(mutable.HashSet.empty) | ||
c.internal.updateAttachment(c.macroApplication, st) | ||
st | ||
case Some(st) => st | ||
} | ||
} | ||
|
||
|
||
class State(val seen: mutable.HashSet[Type]) { | ||
|
||
def infer[F[_]](ftpe: c.Type, atpe: c.Type): c.Tree = { | ||
val makeTc = c.universe.weakTypeOf[Make[F, _]].typeConstructor | ||
val searchType = c.universe.appliedType(makeTc, ftpe, atpe) | ||
c.inferImplicitValue(searchType) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package make.internal | ||
|
||
trait MakeOps extends MakeProductNOps | ||
trait MakeOps extends MakeProductNOps with MakeBasicOps | ||
object MakeOps extends MakeOps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.