Skip to content

Instantly share code, notes, and snippets.

@maxost
Created September 5, 2017 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxost/94298bb9343af2a7c177d9b745c44b8c to your computer and use it in GitHub Desktop.
Save maxost/94298bb9343af2a7c177d9b745c44b8c to your computer and use it in GitHub Desktop.
Kotlin: simple Option implementation
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