Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>extends</code> in generics doesn't really work that way. <code>T</code> != <code>? extends Component</code> even though <code>T extends Component</code>. What you have is in fact a <a href="https://stackoverflow.com/questions/19219635/java-hashmap-nested-generics-with-wildcards">wildcard capture</a>, it has a different purpose.</p> <p>And yes your solution is not type-safe - there is no relation between the two <code>?</code> marks in:</p> <pre><code> private HashMap&lt;Class&lt;?&gt;, HashMap&lt;Entity, ? extends Component&gt;&gt; m_componentStores; </code></pre> <p>So it becomes legal to put an instance of some subclass of <code>Component</code> in this structure using some other class (not even a subclass of <code>Component</code>) as the key.</p> <p>Remember that generic types are resolved at <em>compile time</em> only, so at <em>run time</em> <code>m_componentStores</code> has no way of knowing what exact type of the value you have in there, other than that it <code>extends</code> <code>Component</code>.</p> <p>So the type you get from <code>store.get(e)</code> is ... <code>Component</code>:</p> <pre><code> Component result = store.get(e); </code></pre> <p>When you cast <code>Component</code> to <code>T</code>, the compiler issues a warning because the cast cannot be checked statically. But if you're sure of the semantics of your data structure, you can simply suppress the warning.</p> <pre><code> @SuppressWarnings("unchecked") T resultT = (T)result; </code></pre> <hr> <p>PS: You don't need a wildcard capture, the following will work exactly the same in your case:</p> <pre><code> private HashMap&lt;Class&lt;?&gt;, HashMap&lt;Entity, Component&gt;&gt; m_componentStores; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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