Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ExecutorService and execute Runnable  RSS feed

 
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi all,
I don't understand why line #3 is compiling without any problem. There is only one execute() method in ExecutorService which takes a Runnable, and Runnable run() returns void.
Line #3 shouldn't compile because toString() has an incompatible return type. But it does.
Line #4 does not compile, the compiler error is: bad return type in Lambda expression.
Thank you, Juerg
 
Saloon Keeper
Posts: 8758
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Functional interfaces that have a void return type can always be implemented by lambda expressions that have a statement as their body. Even if that statement is a method call with a return type, the return type is simply ignored.

#3 compiles because rid.toString() is a statement.
#4 does not compile because "TestRiddle" is not a statement.

For the same reason, you can do this:
And not this:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!