Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy not simply disable unchecked warnings?
    text
    copied!<p>When developers interact with non-generic APIs, they usually run into "unchecked" warnings. Consider the following example:</p> <pre><code>import java.util.AbstractList; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class IterableNodeList&lt;T extends Node&gt; extends AbstractList&lt;T&gt; { private NodeList list; public IterableNodeList(NodeList list) { this.list = list; } public T get(int index) { return (T)this.list.item(index); } public int size() { return this.list.getLength(); } } </code></pre> <p>One could of course invest the effort to write this in such a way that there is no warning: using a type parameter <code>T</code> on the class and a constructor argument <code>Class&lt;T&gt;</code>, matching member variable and a <code>cast()</code> call.</p> <p>Alternatively, one could think about simply editing the IDE configuration and build scripts (e.g. Maven POM) to disable this compiler warning entirely. Now if we did that, the code could stay the way it is, but I'm sure that doing must have drawbacks. However, I can't think of any reasonable, realistic examples where</p> <ul> <li>this warning provides more value than "stick on a <code>@SuppressWarnings</code> here, there's no other option anyway", and where</li> <li>the resulting code actually behaves different (and safer) than the one where we ignored (disabled) the warning.</li> </ul> <p>Can you think of such examples or name another reason why disabling these "unchecked" warnings globally is a bad idea? Or is it actually a good idea?</p> <h2>UPDATE</h2> <p><strong>The previous examples did not actually provoke the warnings. Some answers no longer make sense now. Sorry for the inconvenience.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload