-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
146 additions
and
105 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 |
---|---|---|
@@ -1,5 +1,13 @@ | ||
### Changes | ||
|
||
* Introduce the `RefType` type class which abstracts over `shapeless.@@` | ||
and `Refined` and that allows 3rd-party types to be used as result type | ||
for refinements (e.g. [scalaz.@@][scalaz.@@]). `RefType` replaces the | ||
now removed `internal.Wrapper` type class. ([#48], [#53], [#54]) | ||
* Make the `Refined` constructor private ([#52]) | ||
|
||
[#48]: https://github.com/fthomas/refined/issues/48 | ||
[#52]: https://github.com/fthomas/refined/issues/52 | ||
[#53]: https://github.com/fthomas/refined/issues/53 | ||
[#54]: https://github.com/fthomas/refined/issues/54 | ||
[scalaz.@@]: https://github.com/scalaz/scalaz/blob/v7.1.3/core/src/main/scala/scalaz/package.scala#L103 |
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,76 @@ | ||
package eu.timepit.refined | ||
|
||
import eu.timepit.refined.internal.{ RefineAux, RefineMAux } | ||
import shapeless.tag.@@ | ||
|
||
import scala.reflect.macros.blackbox | ||
|
||
trait RefType[F[_, _]] extends Serializable { | ||
|
||
def unsafeWrap[T, P](t: T): F[T, P] | ||
|
||
def unwrap[T, P](tp: F[T, P]): T | ||
|
||
def unsafeWrapM[T: c.WeakTypeTag, P: c.WeakTypeTag](c: blackbox.Context)(t: c.Expr[T]): c.Expr[F[T, P]] | ||
|
||
def unsafeRewrapM[T: c.WeakTypeTag, A: c.WeakTypeTag, B: c.WeakTypeTag](c: blackbox.Context)(ta: c.Expr[F[T, A]]): c.Expr[F[T, B]] | ||
|
||
def refine[P]: RefineAux[F, P] = | ||
new RefineAux[F, P](this) | ||
|
||
def refineM[P]: RefineMAux[F, P] = | ||
new RefineMAux[F, P] | ||
|
||
def mapRefine[T, P, U](tp: F[T, P])(f: T => U)(implicit p: Predicate[P, U]): Either[String, F[U, P]] = | ||
refine(f(unwrap(tp))) | ||
} | ||
|
||
object RefType { | ||
|
||
def apply[F[_, _]](implicit rt: RefType[F]): RefType[F] = rt | ||
|
||
implicit val refinedRefType: RefType[Refined] = | ||
new RefType[Refined] { | ||
override def unsafeWrap[T, P](t: T): Refined[T, P] = | ||
Refined.unsafeApply(t) | ||
|
||
override def unwrap[T, P](tp: Refined[T, P]): T = | ||
tp.get | ||
|
||
override def unsafeWrapM[T: c.WeakTypeTag, P: c.WeakTypeTag](c: blackbox.Context)(t: c.Expr[T]): c.Expr[Refined[T, P]] = | ||
c.universe.reify(Refined.unsafeApply[T, P](t.splice)) | ||
|
||
override def unsafeRewrapM[T: c.WeakTypeTag, A: c.WeakTypeTag, B: c.WeakTypeTag](c: blackbox.Context)(ta: c.Expr[Refined[T, A]]): c.Expr[Refined[T, B]] = | ||
c.universe.reify(ta.splice.asInstanceOf[Refined[T, B]]) | ||
} | ||
|
||
implicit val tagRefType: RefType[@@] = | ||
new RefType[@@] { | ||
override def unsafeWrap[T, P](t: T): T @@ P = | ||
t.asInstanceOf[T @@ P] | ||
|
||
override def unwrap[T, P](tp: T @@ P): T = | ||
tp | ||
|
||
override def unsafeWrapM[T: c.WeakTypeTag, P: c.WeakTypeTag](c: blackbox.Context)(t: c.Expr[T]): c.Expr[T @@ P] = | ||
c.universe.reify(t.splice.asInstanceOf[T @@ P]) | ||
|
||
override def unsafeRewrapM[T: c.WeakTypeTag, A: c.WeakTypeTag, B: c.WeakTypeTag](c: blackbox.Context)(ta: c.Expr[T @@ A]): c.Expr[T @@ B] = | ||
c.universe.reify(ta.splice.asInstanceOf[T @@ B]) | ||
} | ||
|
||
class RefTypeOps[F[_, _], T, P](tp: F[T, P])(implicit F: RefType[F]) { | ||
|
||
def unwrap: T = | ||
F.unwrap(tp) | ||
|
||
def mapRefine[U](f: T => U)(implicit p: Predicate[P, U]): Either[String, F[U, P]] = | ||
F.mapRefine(tp)(f) | ||
} | ||
|
||
object ops { | ||
|
||
implicit def toRefTypeOps[F[_, _]: RefType, T, P](tp: F[T, P]): RefTypeOps[F, T, P] = | ||
new RefTypeOps(tp) | ||
} | ||
} |
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
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
57 changes: 0 additions & 57 deletions
57
shared/src/main/scala/eu/timepit/refined/internal/Wrapper.scala
This file was deleted.
Oops, something went wrong.
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
26 changes: 26 additions & 0 deletions
26
shared/src/test/scala/eu/timepit/refined/RefTypeSpec.scala
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,26 @@ | ||
package eu.timepit.refined | ||
|
||
import eu.timepit.refined.RefType.ops._ | ||
import eu.timepit.refined.numeric.Positive | ||
import org.scalacheck.Prop._ | ||
import org.scalacheck.Properties | ||
import shapeless.tag.@@ | ||
|
||
class RefTypeSpec extends Properties("RefType") { | ||
|
||
def wrapperLaw[F[_, _]](implicit rt: RefType[F]) = forAll { (s: String) => | ||
rt.unwrap(rt.unsafeWrap(s)) == s | ||
} | ||
|
||
property("wrapLaw.Refined") = wrapperLaw[Refined] | ||
|
||
property("wrapLaw.@@") = wrapperLaw[@@] | ||
|
||
property("mapRefine.success") = secure { | ||
RefType[Refined].refineM[Positive](5).mapRefine(_.toDouble).isRight | ||
} | ||
|
||
property("mapRefine.failure") = secure { | ||
RefType[Refined].refineM[Positive](5).mapRefine(_ - 10).isLeft | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
shared/src/test/scala/eu/timepit/refined/WrapperSpec.scala
This file was deleted.
Oops, something went wrong.