Created
September 5, 2017 02:41
-
-
Save maxost/94298bb9343af2a7c177d9b745c44b8c to your computer and use it in GitHub Desktop.
Kotlin: simple Option implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Option<out A> { | |
abstract fun get(): A | |
object None : Option<Nothing>() { | |
override fun get(): Nothing = throw NoSuchElementException("None.get") | |
} | |
data class Some<out A>(val value: A) : Option<A>() { | |
override fun get(): A = value | |
} | |
fun isSome(): Boolean = this is Some | |
fun isNone(): Boolean = this is None | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment