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

Add RuntimeConstraint #175

Merged
merged 9 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 10 additions & 14 deletions main/src/io/github/iltotore/iron/RefinedTypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object RefinedTypeOps:
*/
type FinalType = T

trait RefinedTypeOpsImpl[A, C, T]:
trait RefinedTypeOpsImpl[A, C, T](using rtc: RuntimeConstraint[A, C]):
Iltotore marked this conversation as resolved.
Show resolved Hide resolved
/**
* Implicitly refine at compile-time the given value.
*
Expand Down Expand Up @@ -76,8 +76,8 @@ trait RefinedTypeOpsImpl[A, C, T]:
* @return a [[Right]] containing this value as [[T]] or a [[Left]] containing the constraint message.
* @see [[fromIronType]], [[option]], [[applyUnsafe]].
*/
inline def either(value: A)(using constraint: Constraint[A, C]): Either[String, T] =
Either.cond(constraint.test(value), value.asInstanceOf[T], constraint.message)
inline def either(value: A): Either[String, T] =
Either.cond(rtc.test(value), value.asInstanceOf[T], rtc.message)
Iltotore marked this conversation as resolved.
Show resolved Hide resolved

/**
* Refine the given value at runtime, resulting in an [[Option]].
Expand All @@ -86,8 +86,8 @@ trait RefinedTypeOpsImpl[A, C, T]:
* @return an Option containing this value as [[T]] or [[None]].
* @see [[fromIronType]], [[either]], [[applyUnsafe]].
*/
inline def option(value: A)(using constraint: Constraint[A, C]): Option[T] =
Option.when(constraint.test(value))(value.asInstanceOf[T])
inline def option(value: A): Option[T] =
Option.when(rtc.test(value))(value.asInstanceOf[T])

/**
* Refine the given value at runtime.
Expand All @@ -97,8 +97,8 @@ trait RefinedTypeOpsImpl[A, C, T]:
* @throws an [[IllegalArgumentException]] if the constraint is not satisfied.
* @see [[fromIronType]], [[either]], [[option]].
*/
inline def applyUnsafe(value: A)(using Constraint[A, C]): T =
value.refine[C].asInstanceOf[T]
inline def applyUnsafe(value: A): T =
if rtc.test(value) then value.asInstanceOf[T] else throw new IllegalArgumentException(rtc.message)
Iltotore marked this conversation as resolved.
Show resolved Hide resolved

inline def unapply(value: T): Option[A :| C] = Some(value.asInstanceOf[A :| C])

Expand All @@ -108,12 +108,8 @@ trait RefinedTypeOpsImpl[A, C, T]:

inline given [R]: TypeTest[T, R] = summonInline[TypeTest[A :| C, R]].asInstanceOf[TypeTest[T, R]]

inline given [L](using inline constraint: Constraint[A, C]): TypeTest[L, T] =
val test = summonInline[TypeTest[L, A]]

new TypeTest:
override def unapply(value: L): Option[value.type & T] =
test.unapply(value).filter(constraint.test(_)).asInstanceOf
given [L](using test: TypeTest[L, A]): TypeTest[L, T] with
override def unapply(value: L): Option[value.type & T] = test.unapply(value).filter(rtc.test(_)).asInstanceOf

extension (wrapper: T)
inline def value: IronType[A, C] = wrapper.asInstanceOf[IronType[A, C]]
inline def value: IronType[A, C] = wrapper.asInstanceOf[IronType[A, C]]
8 changes: 8 additions & 0 deletions main/src/io/github/iltotore/iron/RuntimeConstraint.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.iltotore.iron

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

object RuntimeConstraint:
inline given derived[A, B](using c: Constraint[A, B]): RuntimeConstraint[A, B] =
new RuntimeConstraint[A, B](c.test(_), c.message)
Loading