Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kyri-petrou committed Sep 24, 2023
1 parent e2d9ca8 commit e0e0bdd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
9 changes: 6 additions & 3 deletions cats/src/io/github/iltotore/iron/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ object cats extends IronCatsInstances:
* @return a [[Valid]] containing this value as [[IronType]] or an [[Invalid]] containing the constraint message.
* @see [[validatedNec]], [[validatedNel]].
*/
def validated(value: A): Validated[String, T] = ops.either(value).toValidated
def validated(value: A): Validated[String, T] =
if ops.rtc.test(value) then Validated.valid(value.asInstanceOf[T]) else Validated.invalid(ops.rtc.message)

/**
* Refine the given value applicatively at runtime, resulting in a [[ValidatedNec]].
Expand All @@ -154,7 +155,8 @@ object cats extends IronCatsInstances:
* @return a [[Valid]] containing this value as [[IronType]] or an [[Invalid]] containing a [[NonEmptyChain]] of error messages.
* @see [[validated]], [[validatedNel]].
*/
def validatedNec(value: A): ValidatedNec[String, T] = ops.either(value).toValidatedNec
def validatedNec(value: A): ValidatedNec[String, T] =
if ops.rtc.test(value) then Validated.validNec(value.asInstanceOf[T]) else Validated.invalidNec(ops.rtc.message)

/**
* Refine the given value applicatively at runtime, resulting in a [[ValidatedNel]].
Expand All @@ -163,7 +165,8 @@ object cats extends IronCatsInstances:
* @return a [[Valid]] containing this value as [[IronType]] or an [[Invalid]] containing a [[NonEmptyList]] of error messages.
* @see [[validated]], [[validatedNec]].
*/
def validatedNel(value: A): ValidatedNel[String, T] = ops.either(value).toValidatedNel
def validatedNel(value: A): ValidatedNel[String, T] =
if ops.rtc.test(value) then Validated.validNel(value.asInstanceOf[T]) else Validated.invalidNel(ops.rtc.message)

/**
* Represent all Cats' typeclass instances for Iron.
Expand Down
5 changes: 3 additions & 2 deletions main/src/io/github/iltotore/iron/RefinedTypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ object RefinedTypeOps:
*/
type FinalType = T

trait RefinedTypeOpsImpl[A, C, T](using rtc: RuntimeConstraint[A :| C]):
inline given RuntimeConstraint[T] = rtc.asInstanceOf[RuntimeConstraint[T]]

trait RefinedTypeOpsImpl[A, C, T](using rtcAuto: RuntimeConstraint.AutoDerived[A, A :| C]):
given rtc: RuntimeConstraint[A, T] = rtcAuto.inner.asInstanceOf[RuntimeConstraint[A, T]]

/**
* Implicitly refine at compile-time the given value.
Expand Down
15 changes: 9 additions & 6 deletions main/src/io/github/iltotore/iron/RuntimeConstraint.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.github.iltotore.iron

type RuntimeConstraint[T] = T match
case IronType[a, c] => RuntimeConstraint.Impl[a, c, T]
final class RuntimeConstraint[A, T](_test: A => Boolean, val message: String):
inline def test(value: A): Boolean = _test(value)

object RuntimeConstraint:
final class Impl[A, C, T](_test: A => Boolean, val message: String):
inline def test(value: A): Boolean = _test(value)

inline given derived[A, C](using inline c: Constraint[A, C]): RuntimeConstraint[A :| C] =
new Impl[A, C, A :| C](c.test(_), c.message)
inline def derived[A, C](using inline c: Constraint[A, C]): RuntimeConstraint[A, A :| C] =
new RuntimeConstraint[A, A :| C](c.test(_), c.message)

final class AutoDerived[A, T](val inner: RuntimeConstraint[A, T])
object AutoDerived:
inline given derived[A, C](using inline c: Constraint[A, C]): AutoDerived[A, A :| C] =
new AutoDerived[A, A :| C](RuntimeConstraint.derived[A, C])
2 changes: 1 addition & 1 deletion zio/src/io/github/iltotore/iron/zio.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object zio extends RefinedTypeOpsZio:
* @return a [[Valid]] containing this value as [[T]] or an [[Validation.Failure]] containing a [[NonEmptyChunk]] of error messages.
*/
def validation(value: A): Validation[String, T] =
Validation.fromEither(ops.either(value))
Validation.fromPredicateWith(ops.rtc.message)(value)(ops.rtc.test(_)).asInstanceOf[Validation[String, T]]

private trait RefinedTypeOpsZio extends RefinedTypeOpsZioLowPriority:

Expand Down

0 comments on commit e0e0bdd

Please sign in to comment.