Alina Dolgikh [JB]
nkiesel
04/04/2022, 6:52 PMfun <T> getTest(): Optional<T> { return Optional.empty() }
nkiesel
04/04/2022, 6:53 PMfun <T> getEmpty2(): Optional<T> = Optional.empty<T>()
produces the same warning, but fun <T> getList2(): List<T> = emptyList<T>()
does notgildor
04/05/2022, 4:14 AMAlexey Belkov [JB]
04/05/2022, 10:54 AMdenis.zharkov
04/06/2022, 6:41 AMOptional.empty
returns Optional<T & Any>
, i.e. its type argument is strictly not nullable and when it’s used as return expression for Optional<T>
we’ve got type mismatch
The simplest (and probably the most expected) workaround is adding an upper bound to T
fun <T : Any> getTest(): Optional<T> = Optional.empty<T>()
Another option is using definitely not nullable types
fun <T> getTest(): Optional<T & Any> = Optional.empty<T>()