Tech
09/28/2022, 10:22 PMUnit to represent when an action has been preformed but no data is returned, so if it's anything but unit then there's data in the class.
sealed class Test<out T>(val success: Boolean) {
abstract val data: T?
class A<out T>(override val data: T?) : Test(true)
class B<out T>(override val data: T?) : Test(false)
}
fun Test<Any>.isSuccess(): Boolean {
contract {
returns(true) implies(this@isSuccess is Test.A)
// if T != Unit then data != null
}
return this is Test.A
}Youssef Shoaib [MOD]
09/29/2022, 3:21 AMNothing? Type whose only value is null, which makes it quite similar to Unit. What I'm thinking is, instead of having Unit represent an action that returns no data, have Nothing? represent such an action, and make it so that data is non-nullable. Also, you don't need a val success as a member property of Test, especially since you can make your fun isSuccess an extension val instead. Finally, data can be a non-abstract constructor property. All in all, here's what I'm thinking:
sealed class Test<out T>(val data: T) {
class A<out T>(data: T) : Test<T>(data)
class B<out T>(data: T): Test<T>(data)
}
val <T> Test<T>.isSuccess(): Boolean {
contract {
returns(true) implies(this@isSuccess is Test.A)
}
return this is Test.A
}
fun functionWithNoData(): Test<Nothing?> {
println("Nothing to see here, or is it Nothing? (Haha, get it?)")
return Test.A(null)
}
fun answerToLife(isBinary: Boolean) = Test.A(if(isBinary) "000101010" else "42")