Skip to content

Instantly share code, notes, and snippets.

@Cotel
Cotel / RefinedTypes.kt
Last active November 25, 2018 10:35
How to make refined types in Kotlin with Arrow (https://arrow-kt.io/)
import arrow.core.Option
class NonZeroInt private constructor(val value: Int) {
companion object {
operator fun invoke(x: Int): Option<NonZeroInt> =
if (x == 0) Option.empty() else Option.just(NonZeroInt(x))
fun get(x: NonZeroInt): Int = x.value
}
}