title: Call method on chosen method reference inline
body: <p>I have the following program that fails to compile:</p>
  
  <p>Just block 1 compiles fine and works as expected - I can conditionally select an object and call a method on it inline.</p>
  
  <p>Just block 2 also compiles fine and works as expected - I can conditionally assign a method reference to a <code>Supplier&lt;String&gt;</code> variable and call <code>.get()</code> on that variable.</p>
  
  <p>However block 3 fails to compile: </p>
  
  <pre><code>Lambda.java:31: error: method reference not expected here
      String res = ((Supplier&lt;String&gt;) (args.length &gt; 0 ? Lambda::foo : Lambda::bar)).get();
                                                          ^
  Lambda.java:31: error: method reference not expected here
      String res = ((Supplier&lt;String&gt;) (args.length &gt; 0 ? Lambda::foo : Lambda::bar)).get();
  </code></pre>
  
  <p>I would think combining the ideas in block 1 and 2 I would be able to perform block 3 as the type of <code>((Supplier&lt;String&gt;) (args.length &gt; 0 ? Lambda::foo : Lambda::bar))</code> is <code>Supplier&lt;String&gt;</code>. </p>
  
  <pre><code>import java.util.function.Supplier;
  
  class Lambda {
  
    private final String s;
  
    private Lambda(String s) {
      this.s = s;
    }
  
    private static String foo() {
      return "foo";
    }
  
    private static String bar() {
      return "bar";
    }
  
    private String str() {
      return s;
    }
  
    public static void main(String... args) {
      // Block 1
      Lambda l1 = new Lambda("x");
      Lambda l2 = new Lambda("y");
      System.out.println((args.length &gt; 0 ? l1 : l2).str());
  
      // Block 2
      Supplier&lt;String&gt; s = (args.length &gt; 0 ? Lambda::foo : Lambda::bar);
      System.out.println(s.get());
  
      // Block 3
      String res = ((Supplier&lt;String&gt;) (args.length &gt; 0 ? Lambda::foo : Lambda::bar)).get();
      System.out.println(res);
    }
  
  }
  </code></pre>
  
  <p>To be clear: I'm not looking for a workaround here, this wouldn't be good quality code in the first place. I'm just curious why the last block fails to compile. </p>