(Note: I know Kotlin has nullable types - I'm just asking to explore the type system and because Option types have other uses.)
Say I have:
trait Opt<T> { fun get(): T fun isEmpty(): Boolean } class Some<T>(private val x: T) : Opt<T> { override fun get() = x override fun isEmpty() = false } class None<T> : Opt<T> { override fun get() = throw Exception() override fun isEmpty() = true }
This works fine. However, I'm wondering if I can instead define a singleton None, a la Scala. I was hoping this would work since Nothing should be compatible with all types, but it doesn't:
object None : Opt<Nothing> { override fun get() = throw Exception() override fun isEmpty() = true }