For the future, I think I remember `context` will work on the primary constructor, so will something...
c
For the future, I think I remember
context
will work on the primary constructor, so will something like this work?
Copy code
sealed class PasswordValidationError {
    object TooShort : PasswordValidationError()
}

value class Password context(Raise<PasswordValidationError>) constructor(val text: String) {
    init {
        ensure(text.length > 8) { PasswordValidationError.TooShort }
    }
}
s
AFAIK this should indeed work, but note that it might missbehave with KotlinX Serialization. I am not sure how it deals with
CancellationException
, I know it wraps some exceptions but I haven't looked deeply into it.
c
Ah, you're right this might be an issue.
s
If it turns out to be problematic, I would still just use
require
and do something like this: https://github.com/nomisRev/ktor-arrow-example/blob/2ec99199bee7c80633834450f811ff1610a3cfd0/src/main/kotlin/io/github/nomisrev/routes/users.kt#L105
catchOrThrow
is in line with reified
Raise.catch
but for
Either
😉
c
Ah, that does sound like a good idea