object Assertion
An Assertion[A]
is essentially a composable predicate from A => Boolean
.
They can be composed with standard Boolean operators of &&
, ||
and !
.
This is primarily intended to be used with Newtype
and Subtype
,
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).
type Pin = Pin.Type object Pin extends Newtype[Int] { // Scala 2 Syntax def assertion = assert(Assertion.between(1000, 9999)) // Scala 3 Syntax override inline def assertion = Assertion.between(1000, 9999) } // PowerOfTwo(1000) compiles // PowerOfTwo(5412) compiles // PowerOfTwo(34567) fails with "34567 did not satisfy between(1000, 9999)" // PowerOfTwo(234) fails with "123 did not satisfy between(1000, 9999)"
- Alphabetic
- By Inheritance
- Assertion
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- val anything: Assertion[Any]
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def between[A](min: A, max: A)(implicit ordering: Ordering[A]): Assertion[A]
Ensures the value falls between a given min and max (inclusive).
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def contains(string: String): Assertion[String]
- def divisibleBy[A](n: A)(implicit numeric: Numeric[A]): Assertion[A]
- def endsWith(suffix: String): Assertion[String]
- def endsWithIgnoreCase(suffix: String): Assertion[String]
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equalTo[A](value: A): Assertion[A]
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def greaterThan[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]
- def greaterThanOrEqualTo[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]
- def hasLength(lengthAssertion: Assertion[Int]): Assertion[String]
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- val isEmptyString: Assertion[String]
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def lessThan[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]
- def lessThanOrEqualTo[A](value: A)(implicit ordering: Ordering[A]): Assertion[A]
- def matches(regex: scala.util.matching.Regex): Assertion[String]
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)
- def matches(regexString: String): Assertion[String]
- def matches(regex: Regex): Assertion[String]
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- val never: Assertion[Any]
- def notEqualTo[A](value: A): Assertion[A]
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- def powerOf[A](base: A)(implicit numeric: Numeric[A]): Assertion[A]
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
- def startsWith(prefix: String): Assertion[String]
- def startsWithIgnoreCase(prefix: String): Assertion[String]
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
- object Regex