I have this function
fun <T> safe(t: T?): T {
return Optional.ofNullable(t).orElseThrow { IllegalStateException("safe value should not be null") }
}
I use it to say I know that T is not null now so give me back the non null-able form of it.
So I use it like this, I declare
class SomeType(val someOtherType: SomeOtherType?)
But some someOtherType
in another place is declared like so:
class SomeThirdType(val someOtherType: SomeOtherType)
So in SomeType
I have this function:
class SomeType(val someOtherType: SomeOtherType?) {
fun doSomeDamage(): SomeThirdType {
//some work
return SomeThirdType(safe(someOtherType))
}
}
I'm not happy with the safe
function, is there a better way? I feel like I'm missing something fundamental here