Throwing Functions

Brian Goetz brian.goetz at oracle.com
Sat Nov 12 15:36:45 UTC 2022


(corelibs-dev is likely a better place for this, since its not about 
language.)

This was discussed when we did Lambda initially.  There were a few 
reasons we didn't go there:

  - Poor interactions between generics and exceptions;
  - Exceptions and serialization each "wanted" their own versions of the 
functional interfaces; a 4x explosion in java.util.function is not so 
pretty, and doing an ad-hoc "lets just do Function and Consumer" will 
invariably result in an endless stream of "please add ExceptionalFooBar" 
requests.

It's some years later, so its possible we might reconsider; corelibs-dev 
is the right place to ask this.



On 11/11/2022 11:47 PM, Nathan Reynolds wrote:
> I am sorry if I am very late to the discussion.  Consumer, Function, 
> Predicate, and Supplier don't allow for throwing checked exceptions.  
> This feature is needed in many cases. What about adding a variant that 
> allows for throwing checked exceptions?  For example, a 
> ThrowableConsumer could be defined as such...
>
> public interface ThrowableConsumer<T, E extends Throwable>
> {
>    void accept(T t) throws E;
> }
>
> A method that receives this as a parameter could be defined as such...
>
> public <E extends Throwable> void process(ThrowableConsumer<T, E> 
> consume) throws E
> {
>    ...
> }
>
> The compiler takes care of ensuring the checked exception is dealt 
> with in the caller to process().
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20221112/690022f8/attachment.htm>


More information about the amber-dev mailing list