Can Orhan
02/26/2020, 2:17 PMsealed class UserInput<out T : Any> {
data class Value<T : Any>(val output: T) : UserInput<T>()
object Missing : UserInput<Nothing>()
}
The problem with it is that we can’t use Kotlin’s null-safe operators like :?
and .?
.
Do you think there is value in knowing why something is missing? If so, is it worth the loss of null-safe operators?Mike
02/26/2020, 2:57 PMUserInput?
as the type.Mike
02/26/2020, 2:58 PMOptional
for this, as it doesn't allow one to differentiate between a String and a String that may be absent.
Kotlin doesn't require Optional
BECAUSE of null
being part of the type system. So I'd say this is a perfect case where Kotlin is stronger, and one should use the language features.Can Orhan
02/26/2020, 3:05 PMZach Klippenstein (he/him) [MOD]
02/26/2020, 5:47 PMMike
02/26/2020, 6:05 PMinline class
for this. inline class UserInput<T>(val value:T)
. And then define type as UserInput<String>?
when it could be missing, and then once it exists, functions can take the non-null version.Mike
02/26/2020, 6:05 PMZach Klippenstein (he/him) [MOD]
02/26/2020, 6:09 PMMike
02/26/2020, 6:11 PMZach Klippenstein (he/him) [MOD]
02/26/2020, 6:13 PMResult
type works and the compiler is actually generating bytecode for this that looks reasonable, so I think it’s probably one of those “experimental” things.Can Orhan
02/26/2020, 6:15 PMtypealias
for null
but that’s obviously not supported.
I also looked into creating a contract or operator overload where I could express to the compiler that returns(x !is Missing) implies (this is Value)
but couldn’t find anything that’d help