Skip to content

Commit

Permalink
Added type alias to constrain values to a set (Iltotore#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaychandran committed Jun 16, 2023
1 parent 20c29a3 commit 7f3430f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions main/src/io/github/iltotore/iron/constraint/any.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ object any:
*/
final class StrictEqual[V]

/**
* Tests strict equality with any one of the given values.
* @tparam V the values the input must be in.
* @example {{{
* type Digit = In[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)]
* }}}
*/
type In[V <: NonEmptyTuple] = V match
case *:[h, EmptyTuple] => StrictEqual[h]
case *:[h, t] => StrictEqual[h] | In[t]

object True:

inline given [A]: Constraint[A, True] with
Expand Down
6 changes: 6 additions & 0 deletions main/test/src/io/github/iltotore/iron/testing/AnySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ object AnySuite extends TestSuite:
test - 0.assertRefine[StrictEqual[0]]
test - 1.assertNotRefine[StrictEqual[0]]
}

test("in") {
test - 0.assertRefine[In[(0, 1)]]
test - 1.assertRefine[In[(0, 1)]]
test - 2.assertNotRefine[In[(0, 1)]]
}
}

0 comments on commit 7f3430f

Please sign in to comment.