Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #755 - expose Implies[P,C] variation of Inference[P,C] #756

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ object Inference {
): Inference[P, C] =
Inference(i1.isValid && i2.isValid, show.format(i1.show, i2.show))
}

/**
* Similar to `Inference[P, C]` but will not implicitly manifest if `C` cannot be
* inferred from `P`.
*
* It is intended to be used with chained implicit definitions that require proof that `P ==> C`
*/
case class Implies[P, C](show: String)

object Implies {
import scala.reflect.macros.blackbox
import eu.timepit.refined.macros.InferMacro
import Inference.==>

implicit def autoImply[A, B](
implicit
ir: A ==> B
): Implies[A, B] = macro InferMacro.implies[A, B]

def manifest[A: c.WeakTypeTag, B: c.WeakTypeTag](
c: blackbox.Context
)(ir: c.Expr[A ==> B]): c.Expr[Implies[A, B]] =
c.universe.reify(Implies[A, B]((ir.splice).show))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.timepit.refined.macros

import eu.timepit.refined.api.Implies
import eu.timepit.refined.api.Inference.==>
import eu.timepit.refined.api.RefType
import eu.timepit.refined.internal.Resources
Expand All @@ -20,4 +21,12 @@ class InferMacro(val c: blackbox.Context) extends MacroUtils {

refTypeInstance(rt).unsafeRewrapM(c)(ta)
}

def implies[A: c.WeakTypeTag, B: c.WeakTypeTag](ir: c.Expr[A ==> B]): c.Expr[Implies[A, B]] = {
val inference = eval(ir)
if (inference.notValid) {
abort(Resources.invalidInference(weakTypeOf[A].toString, weakTypeOf[B].toString))
}
Implies.manifest(c)(ir)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package eu.timepit.refined

import eu.timepit.refined.api.Refined
import eu.timepit.refined.api.{Implies, Refined}
import eu.timepit.refined.auto._
import eu.timepit.refined.char.{Digit, Letter}
import eu.timepit.refined.generic._
import eu.timepit.refined.numeric.{Greater}
import eu.timepit.refined.types.numeric.PosInt
import org.scalacheck.Prop._
import org.scalacheck.Properties
Expand All @@ -22,6 +23,15 @@ class AutoSpec extends Properties("auto") {
a == b
}

property("autoImply") = secure {
val t = implicitly[Implies[Greater[W.`1`.T], Greater[W.`0`.T]]]
illTyped(
"implicitly[Implies[Greater[W.`-1`.T], Greater[W.`0`.T]]]",
"""type mismatch \(invalid inference\):\s*eu.timepit.refined.numeric.Greater\[Int\(-1\)\] does not imply\s*eu.timepit.refined.numeric.Greater\[Int\(0\)\]"""
)
t.show == "greaterInference(1, 0)"
}

property("autoUnwrap: PosInt: Int") = secure {
val a: PosInt = PosInt.unsafeFrom(1)
val b: Int = a
Expand Down