cactoos icon indicating copy to clipboard operation
cactoos copied to clipboard

Generic exceptions in Func, BiFunc, Proc, BiProc, Scalar, Input and Output

Open SimonHarmonicMinor opened this issue 5 years ago • 3 comments

I consider that these functional interfaces should throw not just an Exception type but generic one. Because such strict bounds make a user to declare a method with too common type.

For instance, suppose I have something like that

public String readResource(Resource resource) throws IOException {
   ...
}
...
Func<String, Resource> func = resource -> readResource(resource);

Even though readResource suppose to throw IOException I have to check for Exception whenever I execute func.

And what if a method threw Throwable? It would force me to catch Throwable inside lambda and rethrow it as an Exception.

I consider that such approach would be more flexible and user friendly:

public interface Func<X, Y, T extends Throwable> {
   Y apply(X value) throws T;
}

SimonHarmonicMinor avatar May 10 '20 11:05 SimonHarmonicMinor

@paulodamaso/z please, pay attention to this issue

0crat avatar May 10 '20 11:05 0crat

@SimonHarmonicMinor This would go against the principles described here 1 2. Since the only good reason for catching is to re-throw, the concrete type of exception is not important.

For this specific case: you may consider using a different type Func<String, Proc<Resource>>, And then combining it with IoCheckedProc or some other Proc decorator that captures your intend for IOException (i.e retry, fail-safe, etc).

andreoss avatar Jun 17 '20 19:06 andreoss

@SimonHarmonicMinor @andreoss I'm not sure for now, but please also see #944 for similar related discussion

victornoel avatar Jul 04 '20 16:07 victornoel