What are the possibilities to design an API that needs overloads depending on a generic type?

时间秒杀一切 提交于 2019-12-04 15:40:30

I have found another way by specifying an extension of Function<A,B> as follows:

class SpecificFunction<A,B> extends Function<A,B> {}

Multiple overloads on Function<A,B> and SpecificFunction<A,C> can be defined then which would be impossible without an additional class because of type erasure. This works because the java language specification explicitly enforces that the java compiler has to bind a function call to the most specific function it can find. One part in the defnition of how to find the most specific function is as follows: if two functions m(a) and n(b) exists m(a) is more specific than n(b) if b is not a subtype of a, which holds for our SpecificFunction<A,B>.

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