-
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.
Add RefinedTypeOps for companion objects of refined types (#369)
Closes #342
- Loading branch information
Showing
10 changed files
with
205 additions
and
9 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
57 changes: 57 additions & 0 deletions
57
modules/core/shared/src/main/scala/eu/timepit/refined/api/RefinedType.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,57 @@ | ||
package eu.timepit.refined | ||
package api | ||
|
||
/** | ||
* Type class that combines `[[RefType]]` and `[[Validate]]` instances | ||
* for a refined type `FTP`. | ||
*/ | ||
trait RefinedType[FTP] { | ||
type F[_, _] | ||
type T | ||
type P | ||
|
||
val refType: RefType[F] | ||
|
||
val validate: Validate[T, P] | ||
|
||
val alias: F[T, P] =:= FTP | ||
|
||
final def refine(t: T): Either[String, FTP] = { | ||
val res = validate.validate(t) | ||
if (res.isPassed) Right(alias(refType.unsafeWrap(t))) | ||
else Left(validate.showResult(t, res)) | ||
} | ||
|
||
final def unsafeRefine(t: T): FTP = | ||
refine(t).fold(err => throw new IllegalArgumentException(err), identity) | ||
} | ||
|
||
object RefinedType { | ||
|
||
def apply[FTP](implicit rt: RefinedType[FTP]): Aux[FTP, rt.F, rt.T, rt.P] = rt | ||
|
||
type Aux[FTP, F0[_, _], T0, P0] = RefinedType[FTP] { | ||
type F[x, y] = F0[x, y] | ||
type T = T0 | ||
type P = P0 | ||
} | ||
|
||
type AuxT[FTP, T0] = RefinedType[FTP] { | ||
type T = T0 | ||
} | ||
|
||
implicit def instance[F0[_, _], T0, P0]( | ||
implicit | ||
rt: RefType[F0], | ||
v: Validate[T0, P0] | ||
): Aux[F0[T0, P0], F0, T0, P0] = | ||
new RefinedType[F0[T0, P0]] { | ||
override type F[x, y] = F0[x, y] | ||
override type T = T0 | ||
override type P = P0 | ||
|
||
override val refType: RefType[F] = rt | ||
override val validate: Validate[T, P] = v | ||
override val alias: F[T, P] =:= F0[T0, P0] = implicitly | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
modules/core/shared/src/main/scala/eu/timepit/refined/api/RefinedTypeOps.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,41 @@ | ||
package eu.timepit.refined | ||
package api | ||
|
||
/** | ||
* Provides functions to create values of the refined type `FTP` from | ||
* values of the base type `T`. It is intended to simplify the definition | ||
* of a refined type's companion object. | ||
* | ||
* Example: {{{ | ||
* scala> import eu.timepit.refined.api.{ Refined, RefinedTypeOps } | ||
* | import eu.timepit.refined.numeric.Positive | ||
* | ||
* scala> type PosInt = Int Refined Positive | ||
* | ||
* scala> object PosInt extends RefinedTypeOps[PosInt, Int] | ||
* | ||
* scala> PosInt(1) | ||
* res0: PosInt = 1 | ||
* | ||
* scala> PosInt.from(2) | ||
* res1: Either[String, PosInt] = Right(2) | ||
* }}} | ||
*/ | ||
class RefinedTypeOps[FTP, T](implicit rt: RefinedType.AuxT[FTP, T]) { | ||
|
||
def apply[F[_, _], P](t: T)( | ||
implicit ev: F[T, P] =:= FTP, | ||
rt: RefType[F], | ||
v: Validate[T, P] | ||
): FTP = | ||
macro macros.RefineMacro.implApplyRef[FTP, F, T, P] | ||
|
||
def from(t: T): Either[String, FTP] = | ||
rt.refine(t) | ||
|
||
def unapply(t: T): Option[FTP] = | ||
from(t).right.toOption | ||
|
||
def unsafeFrom(t: T): FTP = | ||
rt.unsafeRefine(t) | ||
} |
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
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
29 changes: 29 additions & 0 deletions
29
modules/core/shared/src/test/scala/eu/timepit/refined/types/NumericTypesSpec.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,29 @@ | ||
package eu.timepit.refined.types | ||
|
||
import eu.timepit.refined.types.numeric.PosInt | ||
import org.scalacheck.Prop._ | ||
import org.scalacheck.Properties | ||
|
||
class NumericTypesSpec extends Properties("NumericTypes") { | ||
|
||
property("PosInt.from(1)") = secure { | ||
PosInt.from(1) ?= Right(PosInt(1)) | ||
} | ||
|
||
property("PosInt.from(-1)") = secure { | ||
PosInt.from(-1) ?= Left("Predicate failed: (-1 > 0).") | ||
} | ||
|
||
property("PosInt.unapply(1)") = secure { | ||
val PosInt(x) = 1 | ||
x ?= PosInt(1) | ||
} | ||
|
||
property("PosInt.unsafeFrom(1)") = secure { | ||
PosInt.unsafeFrom(1) ?= PosInt(1) | ||
} | ||
|
||
property("PosInt.unsafeFrom(-1)") = secure { | ||
throws(classOf[IllegalArgumentException])(PosInt.unsafeFrom(-1)) | ||
} | ||
} |
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