They are the core principle our of much-beloved Lexi Lambda's "Parse, Don't Validate" approach.
One example might be a dataclass called "NonEmptyAlphanumericString" in Kotlin:
data class NonEmptyAlphanumericString(val value: String) {
init {
require(value.isNotBlank()) { "String cannot be blank" }
require(value.all { it.isLetterOrDigit() }) { "String must be alphanumeric" }
}
}
Here we have a data type that is parsed from a basic "String" and enforces the invariants that the string is neither empty, nor contains non-alphanumeric characters.If it fails those invariants, we won't get a valid instance of "NonEmptyAlphanumericString" and "require()" will throw.
(Though is this is a bit different because there is purposefully no mutable state here, if you enforce invariants again it's through creating a new instance of the immutable class)