Optional<> and return type narrowing

亡梦爱人 提交于 2019-12-07 01:10:22

问题


In Java < 8, returning "unsafe" objects (objects or null), I was able to specialize return type in subclass:

class A {}
class B extends A {}
interface Sup { A a(); /* returns A instance, or null */ }
interface Sub extends Sup { B a(); }

In Java 8, if I want to make my API "safer", I should return Optional<A> instead of "raw" A:

interface Sup { Optional<A> a(); }
interface Sub extends Sup { Optional<B> a(); }

But doesn't compile! Because Optional<B> is not a subclass of Optional<A>.

How I'm supposed to resolve this issue?


回答1:


You could use wildcards.

interface Sup { Optional<? extends A> a(); }

interface Sub extends Sup { Optional<? extends B> a(); }

I could have made it just Optional<B> but using Optional<? extends B> allows another interface to extend Sub and do the same thing.

Personally, I think this is a bit of a mess, and it would be preferable to just return A or B, or null where necessary.




回答2:


Change your parent bounds to use wildcards:

Optional<? extends A> // parent
Optional<? extends B> // child

The reason that your code doesn't work now is due to the fact that generics are invariant. B is-an A, but Optional<B> is not an Optional<A>.



来源:https://stackoverflow.com/questions/29869548/optional-and-return-type-narrowing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!