Skip to content

Commit 91d414a

Browse files
committed
Adding checkedApply to all function variants to support implicit throws
1 parent f240f84 commit 91d414a

File tree

168 files changed

+473
-273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+473
-273
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ might need to be reworked, and subtyping is obviously no longer supported.
1616
type is now the supertype of `Lens` and `Iso`, and `lens` package has been moved to `optics`
1717
- ***Breaking Change***: `Try` and `Either` no longer preserve `Throwable` type since it was inherently not type-safe
1818
anyway; Try is therefore no longer a `Bifunctor`, and `orThrow` can be used to declare checked
19-
exceptions that could be caught by corresponding catch blocks
19+
exceptions that could be caught by corresponding catch blocks
20+
- ***Breaking Change***: All `Fn*` types target methods now support throwing `Throwable`; `apply` is now defaulted and
21+
will simply bypass javac to throw checked exceptions as if they were unchecked. This allows all
22+
checked variants to be eliminated
2023
- `IO` is now stack-safe, regardless of whether the composition nests linearly or recursively
2124

2225
### Added

src/main/java/com/jnape/palatable/lambda/adt/Either.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import com.jnape.palatable.lambda.adt.choice.Choice3;
44
import com.jnape.palatable.lambda.adt.coproduct.CoProduct2;
5+
import com.jnape.palatable.lambda.functions.Fn1;
56
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek;
67
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek2;
7-
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedFn1;
88
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedRunnable;
99
import com.jnape.palatable.lambda.functions.specialized.checked.CheckedSupplier;
1010
import com.jnape.palatable.lambda.functor.Applicative;
@@ -83,7 +83,7 @@ public final L forfeit(Function<? super R, ? extends L> forfeitFn) {
8383
* @throws T the result of applying the wrapped left value to throwableFn, if this is a left
8484
*/
8585
public final <T extends Throwable> R orThrow(Function<? super L, ? extends T> throwableFn) throws T {
86-
return match((CheckedFn1<T, L, R>) l -> {
86+
return match((Fn1<L, R>) l -> {
8787
throw throwableFn.apply(l);
8888
}, id());
8989
}

src/main/java/com/jnape/palatable/lambda/adt/Try.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public static Try<Unit> trying(CheckedRunnable<?> runnable) {
292292
@SuppressWarnings("try")
293293
public static <A extends AutoCloseable, B> Try<B> withResources(
294294
CheckedSupplier<? extends Exception, A> aSupplier,
295-
CheckedFn1<? extends Exception, ? super A, ? extends Try<? extends B>> fn) {
295+
CheckedFn1<?, ? super A, ? extends Try<? extends B>> fn) {
296296
return trying(() -> {
297297
try (A resource = aSupplier.get()) {
298298
return fn.apply(resource).<B>fmap(upcast());
@@ -316,7 +316,8 @@ public static <A extends AutoCloseable, B extends AutoCloseable, C> Try<C> withR
316316
CheckedSupplier<? extends Exception, ? extends A> aSupplier,
317317
CheckedFn1<? extends Exception, ? super A, ? extends B> bFn,
318318
CheckedFn1<? extends Exception, ? super B, ? extends Try<? extends C>> fn) {
319-
return withResources(aSupplier, a -> withResources(() -> bFn.apply(a), fn::apply));
319+
CheckedFn1<Throwable, A, Try<? extends C>> checkedFn = a -> withResources(() -> bFn.apply(a), fn::apply);
320+
return withResources(aSupplier, checkedFn);
320321
}
321322

322323
/**
@@ -339,7 +340,8 @@ public static <A extends AutoCloseable, B extends AutoCloseable, C extends AutoC
339340
CheckedFn1<? extends Exception, ? super A, ? extends B> bFn,
340341
CheckedFn1<? extends Exception, ? super B, ? extends C> cFn,
341342
CheckedFn1<? extends Exception, ? super C, ? extends Try<? extends D>> fn) {
342-
return withResources(aSupplier, bFn, b -> withResources(() -> cFn.apply(b), fn::apply));
343+
CheckedFn1<Exception, B, Try<? extends D>> checkedFn = b -> withResources(() -> cFn.apply(b), fn::apply);
344+
return withResources(aSupplier, bFn, checkedFn);
343345
}
344346

345347
private static final class Failure<A> extends Try<A> {

src/main/java/com/jnape/palatable/lambda/functions/Effect.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.functions;
22

33
import com.jnape.palatable.lambda.adt.Unit;
4+
import com.jnape.palatable.lambda.functions.specialized.checked.Runtime;
45
import com.jnape.palatable.lambda.functor.Applicative;
56
import com.jnape.palatable.lambda.io.IO;
67

@@ -20,11 +21,27 @@
2021
@FunctionalInterface
2122
public interface Effect<A> extends Fn1<A, IO<Unit>>, Consumer<A> {
2223

24+
void checkedAccept(A a) throws Throwable;
25+
26+
@Override
27+
default void accept(A a) {
28+
try {
29+
checkedAccept(a);
30+
} catch (Throwable t) {
31+
throw Runtime.throwChecked(t);
32+
}
33+
}
34+
2335
@Override
2436
default IO<Unit> apply(A a) {
2537
return io(() -> accept(a));
2638
}
2739

40+
@Override
41+
default IO<Unit> checkedApply(A a) throws Throwable {
42+
return io(() -> accept(a));
43+
}
44+
2845
@Override
2946
default <Z> Effect<Z> diMapL(Function<? super Z, ? extends A> fn) {
3047
return effect(Fn1.super.diMapL(fn));

src/main/java/com/jnape/palatable/lambda/functions/Fn0.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,23 @@
2121
@FunctionalInterface
2222
public interface Fn0<A> extends Fn1<Unit, A>, Supplier<A>, Callable<A> {
2323

24-
A apply();
24+
A checkedApply() throws Throwable;
2525

2626
/**
27-
* Invoke this function with {@link Unit}.
27+
* Convenience method for applying this {@link Fn0} without providing an explicit {@link Unit}.
2828
*
29-
* @param unit the only allowed input
30-
* @return the result value
29+
* @return the result
30+
*/
31+
default A apply() {
32+
return apply(UNIT);
33+
}
34+
35+
/**
36+
* {@inheritDoc}
3137
*/
3238
@Override
33-
default A apply(Unit unit) {
34-
return apply();
39+
default A checkedApply(Unit unit) throws Throwable {
40+
return checkedApply();
3541
}
3642

3743
@Override

src/main/java/com/jnape/palatable/lambda/functions/Fn1.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.jnape.palatable.lambda.adt.Either;
44
import com.jnape.palatable.lambda.adt.choice.Choice2;
55
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
6+
import com.jnape.palatable.lambda.functions.specialized.checked.Runtime;
67
import com.jnape.palatable.lambda.functor.Applicative;
78
import com.jnape.palatable.lambda.functor.Cartesian;
89
import com.jnape.palatable.lambda.functor.Cocartesian;
@@ -30,12 +31,26 @@ public interface Fn1<A, B> extends
3031
Function<A, B> {
3132

3233
/**
33-
* Invoke this function with the given argument.
34+
* Invoke this function explosively with the given argument.
3435
*
3536
* @param a the argument
3637
* @return the result of the function application
3738
*/
38-
B apply(A a);
39+
default B apply(A a) {
40+
try {
41+
return checkedApply(a);
42+
} catch (Throwable t) {
43+
throw Runtime.throwChecked(t);
44+
}
45+
}
46+
47+
/**
48+
* Invoke this function with the given argument, potentially throwing any {@link Throwable}.
49+
*
50+
* @param a the argument
51+
* @return the result of the function application
52+
*/
53+
B checkedApply(A a) throws Throwable;
3954

4055
/**
4156
* Convert this {@link Fn1} to an {@link Fn0} by supplying an argument to this function. Useful for fixing an

src/main/java/com/jnape/palatable/lambda/functions/Fn2.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.functions;
22

33
import com.jnape.palatable.lambda.adt.product.Product2;
4+
import com.jnape.palatable.lambda.functions.specialized.checked.Runtime;
45
import com.jnape.palatable.lambda.functor.Applicative;
56

67
import java.util.function.BiFunction;
@@ -23,14 +24,30 @@
2324
@FunctionalInterface
2425
public interface Fn2<A, B, C> extends Fn1<A, Fn1<B, C>> {
2526

27+
C checkedApply(A a, B b) throws Throwable;
28+
2629
/**
2730
* Invoke this function with the given arguments.
2831
*
2932
* @param a the first argument
3033
* @param b the second argument
3134
* @return the result of the function application
3235
*/
33-
C apply(A a, B b);
36+
default C apply(A a, B b) {
37+
try {
38+
return checkedApply(a, b);
39+
} catch (Throwable t) {
40+
throw Runtime.throwChecked(t);
41+
}
42+
}
43+
44+
/**
45+
* {@inheritDoc}
46+
*/
47+
@Override
48+
default Fn1<B, C> checkedApply(A a) throws Throwable {
49+
return b -> checkedApply(a, b);
50+
}
3451

3552
/**
3653
* {@inheritDoc}

src/main/java/com/jnape/palatable/lambda/functions/Fn3.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.functions;
22

33
import com.jnape.palatable.lambda.adt.product.Product2;
4+
import com.jnape.palatable.lambda.functions.specialized.checked.Runtime;
45
import com.jnape.palatable.lambda.functor.Applicative;
56

67
import java.util.function.BiFunction;
@@ -21,6 +22,8 @@
2122
@FunctionalInterface
2223
public interface Fn3<A, B, C, D> extends Fn2<A, B, Fn1<C, D>> {
2324

25+
D checkedApply(A a, B b, C c) throws Throwable;
26+
2427
/**
2528
* Invoke this function with the given arguments.
2629
*
@@ -29,7 +32,21 @@ public interface Fn3<A, B, C, D> extends Fn2<A, B, Fn1<C, D>> {
2932
* @param c the third argument
3033
* @return the result of the function application
3134
*/
32-
D apply(A a, B b, C c);
35+
default D apply(A a, B b, C c) {
36+
try {
37+
return checkedApply(a, b, c);
38+
} catch (Throwable t) {
39+
throw Runtime.throwChecked(t);
40+
}
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
@Override
47+
default Fn1<C, D> checkedApply(A a, B b) throws Throwable {
48+
return c -> checkedApply(a, b, c);
49+
}
3350

3451
/**
3552
* {@inheritDoc}

src/main/java/com/jnape/palatable/lambda/functions/Fn4.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.functions;
22

33
import com.jnape.palatable.lambda.adt.product.Product2;
4+
import com.jnape.palatable.lambda.functions.specialized.checked.Runtime;
45
import com.jnape.palatable.lambda.functor.Applicative;
56

67
import java.util.function.BiFunction;
@@ -22,6 +23,8 @@
2223
@FunctionalInterface
2324
public interface Fn4<A, B, C, D, E> extends Fn3<A, B, C, Fn1<D, E>> {
2425

26+
E checkedApply(A a, B b, C c, D d) throws Throwable;
27+
2528
/**
2629
* Invoke this function with the given arguments.
2730
*
@@ -31,7 +34,21 @@ public interface Fn4<A, B, C, D, E> extends Fn3<A, B, C, Fn1<D, E>> {
3134
* @param d the fourth argument
3235
* @return the result of the function application
3336
*/
34-
E apply(A a, B b, C c, D d);
37+
default E apply(A a, B b, C c, D d) {
38+
try {
39+
return checkedApply(a, b, c, d);
40+
} catch (Throwable t) {
41+
throw Runtime.throwChecked(t);
42+
}
43+
}
44+
45+
/**
46+
* {@inheritDoc}
47+
*/
48+
@Override
49+
default Fn1<D, E> checkedApply(A a, B b, C c) throws Throwable {
50+
return d -> checkedApply(a, b, c, d);
51+
}
3552

3653
/**
3754
* {@inheritDoc}

src/main/java/com/jnape/palatable/lambda/functions/Fn5.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.functions;
22

33
import com.jnape.palatable.lambda.adt.product.Product2;
4+
import com.jnape.palatable.lambda.functions.specialized.checked.Runtime;
45
import com.jnape.palatable.lambda.functor.Applicative;
56

67
import java.util.function.BiFunction;
@@ -23,6 +24,8 @@
2324
@FunctionalInterface
2425
public interface Fn5<A, B, C, D, E, F> extends Fn4<A, B, C, D, Fn1<E, F>> {
2526

27+
F checkedApply(A a, B b, C c, D d, E e) throws Throwable;
28+
2629
/**
2730
* Invoke this function with the given arguments.
2831
*
@@ -33,7 +36,22 @@ public interface Fn5<A, B, C, D, E, F> extends Fn4<A, B, C, D, Fn1<E, F>> {
3336
* @param e the fifth argument
3437
* @return the result of the function application
3538
*/
36-
F apply(A a, B b, C c, D d, E e);
39+
default F apply(A a, B b, C c, D d, E e) {
40+
try {
41+
return checkedApply(a, b, c, d, e);
42+
} catch (Throwable t) {
43+
throw Runtime.throwChecked(t);
44+
}
45+
46+
}
47+
48+
/**
49+
* {@inheritDoc}
50+
*/
51+
@Override
52+
default Fn1<E, F> checkedApply(A a, B b, C c, D d) throws Throwable {
53+
return e -> checkedApply(a, b, c, d, e);
54+
}
3755

3856
/**
3957
* {@inheritDoc}

0 commit comments

Comments
 (0)