• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Callable Lambda

 
Ranch Hand
Posts: 153
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am looking for an explanation why the following line compiles



I would have expected that a lambda that implements Callable<Integer> must return either void, int or Integer since

 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would have to return Integer; you can't turn a formal type parameter into a primitive. I suggest you start by writing a method (the shorter the better) which includes that code, and view its bytecode with javap.
 
Author
Posts: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at the bytecode, really? Do you mean you can compile it too? We might not be using the same compiler. Mine says: Error:(l, c) java: incompatible types: bad return type in lambda expression
   missing return value!
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Markus Schmider said that code compiles, so I believed him. It is possible that he has a different Callable interface.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found that javac (jdk1.8.0) compiles it without reporting an error; NetBeans on the other hand flags the error mentioned by Pierre-Yves.
 
Pierre-Yves Saumont
Author
Posts: 161
31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried with Oracle JDK 1.8.0_121-b13, Oracle JDK 1.8.0_92-b14, Oracle JDK 1.8.0_77-b03,  OpenJDK build 1.8.0_111-b16, and none of these compile this code without error. I would be interested in knowing he exact version used.
 
Marshal
Posts: 26908
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to confirm, I'm using java.util.concurrent.Callable.

And my Eclipse compiler (using Java 8) says "This method must return a result of type Integer" (which it clearly doesn't) and offers as a quick fix to add a return statement (which fixes the problem).
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eclipse uses a different compiler, which often produces better error messages.
But what does the JLS say?

OP: Please post a complete class with that code in and which compiles.

javac WontCompile.java
WontCompile.java:5: error: incompatible types: bad return type in lambda expression
   Callable<Integer> c = () -> {};
                         ^
   missing return value
1 error

If I replace the {} with 999 it compiles all right. JDK1.8.0_121 from Oracle.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pierre-Yves Saumont wrote:I would be interested in knowing he exact version used.




Looks like a bug that was fixed in later updates.
reply
    Bookmark Topic Watch Topic
  • New Topic