I have a use case where I need double level of nullable, something like x: SomeClass??. See prefetchedRecord argument in java code below
public Optional<SomeClass> fetchRecord(Long id) {
...
}
public void process(
Long id,
Optional<Optional<SomeClass>> prefetchedRecord
) {
Optional<SomeClass> fetchedRecord = prefetchedResult.orElseGet( () -> fetchRecord(id) )
if (fetchedRecord.isPresent()) { ... process ... }
else { ... do something else ... }
}
So for the prefetchedRecord the inner optional signifies the result of the fetching and the outer optional shows whether the fetching was ever done before calling the process function. I use two optional to avoid refetching the record even if the fetching returned nothing.
How would you write this in Kotlin? I realize using double optional is not that clear and I could use a custom class instead, smth like
data class FetchResult<T>(val result: T, val fetched: boolean)
but I wonder if there is something built in into Kotlin to support this use case.