Ensures the value falls between a given min and max (inclusive).
Matches a scala.util.matching.Regex.
Matches a scala.util.matching.Regex.
In order to use this for compile-time Assertions, make sure to use the string literal extension method, e.g.:
Assertion.matches("\\w+@\\d{3,5}".r)
Ensures that the value is a power of the given base.
Ensures that the value is a power of the given base.
type PowerOfTwo = PowerOfTwo.Type object PowerOfTwo extends Newtype[Int] { def assertion = assert(Assertion.powerOf(2)) } // PowerOfTwo(1024) compiles // PowerOfTwo(1025) fails
An
Assertion[A]is essentially a composable predicate fromA => Boolean. They can be composed with standard Boolean operators of&&,||and!. This is primarily intended to be used withNewtypeandSubtype, enhancing them with compile-time time validation.For example, if you'd like to validate that a particular Int is precisely 4 digits long, you can create the following refined Newtype. (Note that the syntax is slightly difference between Scala 2 and Scala 3).