Why Doesn’t Java 8 Type Inference Consider Exceptions Thrown by Lambdas in Overload Selection?

If it makes you feel any better, this topic was indeed carefully considered during the JSR-335 design process.

The question is not “why isn’t it able”, but “why did we choose not to.” When we find multiple potentially applicable overloads, we certainly could have chosen to speculatively attribute the lambda body under each set of signatures, and prune those candidates for which the lambda body failed to type-check.

However, we concluded that doing so would likely do more harm than good; it means, for example, that small changes to the method body, under this rule, could cause some method overload selection decisions to silently change without the user intending to do so. In the end, we concluded that using the presence of errors in the method body to discard a potentially applicable candidate would cause more confusion than benefit, especially given that there is a simple and safe workaround — provide a target-type. We felt that reliability and predictability here outweighed optimal concision.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)