-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
193b93d
commit de20278
Showing
4 changed files
with
59 additions
and
42 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
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,24 @@ | ||
package io.github.iltotore.iron | ||
|
||
import scala.util.NotGiven | ||
|
||
/** | ||
* A [[RuntimeConstraint]] is similar to a [[Constraint]] with the difference that it can be used | ||
* in non-inlined methods. | ||
* | ||
* This allows refinement of values in polymorphic methods / givens without the use of `inline`. | ||
* e.g., the code below would fail to compile if [[Constraint]] was used instead. | ||
* | ||
* {{{ | ||
* def foo[A, C](value: A)(using c: RuntimeConstraint[A, C]): Either[String, A :| C] = | ||
* if c.test(value) then Right(value.assume[C]) else Left(c.message) | ||
* }}} | ||
* | ||
* In cases that one does not exist in scope, one will be automatically derived from a [[Constraint]]. | ||
*/ | ||
final class RuntimeConstraint[A, C](_test: A => Boolean, val message: String): | ||
inline def test(value: A): Boolean = _test(value) | ||
|
||
object RuntimeConstraint: | ||
inline given derived[A, C](using inline c: Constraint[A, C]): RuntimeConstraint[A, C] = | ||
new RuntimeConstraint[A, C](c.test(_), c.message) |
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