Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed Jul 26, 2016
1 parent 5faef9e commit e79ee1d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.timepit.refined
package scalacheck

import eu.timepit.refined.api.{ RefType, Validate }
import eu.timepit.refined.api.{ RefinedType, RefType }
import eu.timepit.refined.numeric._
import eu.timepit.refined.scalacheck.util.{ Adjacent, Bounded }
import org.scalacheck.{ Arbitrary, Gen }
Expand All @@ -22,12 +22,13 @@ object numeric {
*
* This is like ScalaCheck's `Gen.chooseNum` but for refined types.
*/
def chooseRefinedNum[F[_, _], T: Numeric: Choose, P](min: F[T, P], max: F[T, P])(
def chooseRefinedNum[FTP, T: Numeric: Choose](min: FTP, max: FTP)(
implicit
rt: RefType[F],
v: Validate[T, P]
): Gen[F[T, P]] =
Gen.chooseNum(rt.unwrap(min), rt.unwrap(max)).filter(v.isValid).map(rt.unsafeWrap)
rt: RefinedType.AuxT[FTP, T]
): Gen[FTP] =
Gen.chooseNum(rt.unwrap(min), rt.unwrap(max))
.filter(rt.validate.isValid)
.map(rt.wrapUnsafe)

///

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ trait RefinedType[FTP] {

val validate: Validate[T, P]

val subst1: F[T, P] =:= FTP
val alias: F[T, P] =:= FTP

val subst2: FTP =:= F[T, P]
val unalias: FTP =:= F[T, P]

def refine(t: T): Either[String, FTP] = {
val res = validate.validate(t)
if (res.isPassed) Right(subst1(refType.unsafeWrap(t)))
if (res.isPassed) Right(wrapUnsafe(t))
else Left(validate.showResult(t, res))
}

Expand All @@ -29,7 +29,10 @@ trait RefinedType[FTP] {
): FTP = macro macros.RefineMacro.refineImpl[FTP, T, P]

def unwrap(tp: FTP): T =
refType.unwrap(subst2(tp))
refType.unwrap(unalias(tp))

def wrapUnsafe(t: T): FTP =
alias(refType.unsafeWrap(t))
}

object RefinedType {
Expand Down Expand Up @@ -57,7 +60,7 @@ object RefinedType {

override val refType: RefType[F] = rt
override val validate: Validate[T, P] = v
override val subst1: F[T, P] =:= F0[T, P0] = implicitly
override val subst2: F0[T0, P0] =:= F[T, P] = implicitly
override val alias: F[T, P] =:= F0[T, P0] = implicitly
override val unalias: F0[T0, P0] =:= F[T, P] = implicitly
}
}

0 comments on commit e79ee1d

Please sign in to comment.