Why use a wild card capture helper method?

后端 未结 4 1006
感动是毒
感动是毒 2020-12-31 07:24

Referring to : Wildcard Capture Helper Methods

It says to create a helper method to capture the wild card.

public void foo(List i) {
    fo         


        
4条回答
  •  有刺的猬
    2020-12-31 08:17

    I agree: Delete the helper method and type the public API. There's no reason not to, and every reason to.

    Just to summarise the need for the helper with the wildcard version: Although it's obvious to us as humans, the compiler doesn't know that the unknown type returned from l.get(0) is the same unknown type of the list itself. ie it doesn't factor in that the parameter of the set() call comes from the same list object as the target, so it must be a safe operation. It only notices that the type returned from get() is unknown and the type of the target list is unknown, and two unknowns are not guaranteed to be the same type.

提交回复
热议问题