Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>First attempt</h2> <p>(Does not work, see comments)</p> <p>Try this way:</p> <pre><code>public static &lt;A extends Entity, B extends Entity&gt; void doubleRequestExecute(Request&lt;A&gt; request1, Request&lt;B&gt; request2, final Callback&lt;Pair&lt;? super A, ? super B&gt;&gt; callback) ... callback.onSuccess(new Pair&lt;A, B&gt;(result, result2.value)); ... </code></pre> <h2>Second attempt</h2> <p>I think I got it now. Here:</p> <pre><code>public static &lt;A extends Entity, B extends Entity&gt; void doubleRequestExecute(Request&lt;A&gt; request1, Request&lt;B&gt; request2, final Callback&lt;? super Pair&lt;? extends A, ? extends B&gt;&gt; callback) { ... } </code></pre> <p>Example usage:</p> <pre><code>private static final class E1 extends Entity { } private static final class E2 extends Entity { } public static void main(String[] args) { Request&lt;E1&gt; r1 = null; Request&lt;E2&gt; r2 = null; // This is the callback you try to define in your comment Callback&lt;Pair&lt;? extends Entity, ? extends Entity&gt;&gt; cb = null; // This is another option Callback&lt;Object&gt; cb2 = null; // Further option mentioned in comments Callback&lt;Pair&lt;? extends Object, ? extends Object&gt;&gt; cb3 = null; doubleRequestExecute(r1, r2, cb); doubleRequestExecute(r1, r2, cb2); doubleRequestExecute(r1, r2, cb3); } </code></pre> <h2>Why can't I simply use <code>Callback&lt;Pair&lt;Object, Object&gt;&gt;</code>?</h2> <p>This is the same problem as why <code>List&lt;Integer&gt;</code> is not assignable to <code>List&lt;Number&gt;</code>. Consider this code:</p> <pre><code>Pair&lt;Integer, Integer&gt; pII = new Pair&lt;Integer, Integer&gt;(); Pair&lt;Object, Object&gt; pOO = pII; // Compile error pOO.setFirst("Whoa"); Integer i = pII.getFirst(); </code></pre> <p>I understand your <code>Pair</code>s have no setter method, but the definition of Java generics imply that a <code>Pair&lt;A, B&gt;</code> is not assignable to a <code>Pair&lt;Object, Object&gt;</code>, but only to a <code>Pair&lt;? extends Object, ? extends Object&gt;</code>.</p> <h2>I want <code>Callback&lt;Pair&lt;? super A, ? super B&gt;&gt;</code>!</h2> <p>Unfortunately, you can't instantiate a type with that signature in any useful (to you) way. Consider again <code>List</code>s.</p> <pre><code>List&lt;List&lt;? super Integer&gt;&gt; l = null; // TODO l.add(new ArrayList&lt;Integer&gt;()); l.add(new ArrayList&lt;Number&gt;()); l.add(new ArrayList&lt;Objec&gt;()); </code></pre> <p>How do you instantiate <code>l</code>? It is a list of lists composed of something bounded by <code>Integer</code>. </p> <pre><code>List&lt;List&lt;? super Integer&gt;&gt; l = new ArrayList&lt;List&lt;Integer&gt;&gt;(); // NO List&lt;List&lt;? super Integer&gt;&gt; l = new ArrayList&lt;List&lt;Number&gt;&gt;(); // NOPE List&lt;List&lt;? super Integer&gt;&gt; l = new ArrayList&lt;List&lt;Object&gt;&gt;(); // NEITHER List&lt;List&lt;? super Integer&gt;&gt; l = new ArrayList&lt;List&lt;? super Integer&gt;&gt;(); // OK </code></pre> <p>Same goes for your callback.</p> <pre><code>// Only possible declaration Callback&lt;Pair&lt;? super E1, ? super E2&gt;&gt; cb = new Callback&lt;Pair&lt;? super E1, ? super E2&gt;&gt;() { ... } </code></pre> <h2>Example use</h2> <pre><code>doubleRequestExecute(new Request&lt;E1&gt;(), new Request&lt;E2&gt;(), new Callback&lt;Pair&lt;? extends Object, ? extends Object&gt;&gt;() { @Override public void onSuccess(Pair&lt;? extends Object, ? extends Object&gt; result) {} @Override public void onFailure(Throwable caught) {} }); </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.
 

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