-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
(From a conversation on Slack)
The idea is to automatically provide conversions or constructors from basic types to classes with invariants.Some ideas:
- For
value class
we create from the base type to the type with invariants, as in the case above. Maybe we should returnString?
instead.
@JvmInline
value class String5(val value: String) {
init {
require(value.length < 5)
}
}
fun String.asString5() = if (this.length < 5) {
String5(this).right()
} else {
"String '$this' should be smaller than 5".left()
}
- Conversely, we could add pseudo-constructors in the companion object which return the type but nullable.
@JvmInline
value class String5(val value: String) {
init {
require(value.length < 5)
}
companion object {
fun attempt(value: String): String5? = ...
}
}
Metadata
Metadata
Assignees
Labels
No labels