Skip to content

Commit 4796989

Browse files
committed
Try/Either no longer attempt in vain to preserve Throwable type
1 parent 45724d5 commit 4796989

7 files changed

Lines changed: 96 additions & 116 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ might need to be reworked, and subtyping is obviously no longer supported.
1414
- ***Breaking Change***: `Strong` is now called `Cartesian` to better reflect the type of strength
1515
- ***Breaking Change***: new Optic type hierarchy more faithfully encodes profunctor constraints on optics, new `Optic`
1616
type is now the supertype of `Lens` and `Iso`, and `lens` package has been moved to `optics`
17+
- ***Breaking Change***: Try and Either no longer preserve `Throwable` type since it was inherently not type-safe
18+
anyway; Try is therefore no longer a `Bifunctor`
1719
- `IO` is now stack-safe, regardless of whether the composition nests linearly or recursively
1820

1921
### Added

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -323,26 +323,24 @@ public static <L, R> Either<L, R> fromMaybe(Maybe<R> maybe, Supplier<L> leftFn)
323323
*
324324
* @param supplier the supplier of the right value
325325
* @param leftFn a function mapping E to L
326-
* @param <T> the most contravariant exception that the supplier might throw
327326
* @param <L> the left parameter type
328327
* @param <R> the right parameter type
329328
* @return the supplier result as a right value, or leftFn's mapping result as a left value
330329
*/
331-
public static <T extends Throwable, L, R> Either<L, R> trying(CheckedSupplier<T, ? extends R> supplier,
332-
Function<? super T, ? extends L> leftFn) {
333-
return Try.<T, R>trying(supplier::get).toEither(leftFn);
330+
public static <L, R> Either<L, R> trying(CheckedSupplier<?, ? extends R> supplier,
331+
Function<? super Throwable, ? extends L> leftFn) {
332+
return Try.<Throwable, R>trying(supplier::get).toEither(leftFn);
334333
}
335334

336335
/**
337336
* Attempt to execute the {@link CheckedSupplier}, returning its result in a right value. If the supplier throws an
338337
* exception, wrap it in a left value and return it.
339338
*
340339
* @param supplier the supplier of the right value
341-
* @param <T> the left parameter type (the most contravariant exception that supplier might throw)
342340
* @param <R> the right parameter type
343341
* @return the supplier result as a right value, or a left value of the thrown exception
344342
*/
345-
public static <T extends Throwable, R> Either<T, R> trying(CheckedSupplier<T, R> supplier) {
343+
public static <R> Either<Throwable, R> trying(CheckedSupplier<?, R> supplier) {
346344
return trying(supplier, id());
347345
}
348346

@@ -352,12 +350,11 @@ public static <T extends Throwable, R> Either<T, R> trying(CheckedSupplier<T, R>
352350
*
353351
* @param runnable the runnable
354352
* @param leftFn a function mapping E to L
355-
* @param <T> the most contravariant exception that the runnable might throw
356353
* @param <L> the left parameter type
357354
* @return {@link Unit} as a right value, or leftFn's mapping result as a left value
358355
*/
359-
public static <T extends Throwable, L> Either<L, Unit> trying(CheckedRunnable<T> runnable,
360-
Function<? super T, ? extends L> leftFn) {
356+
public static <L> Either<L, Unit> trying(CheckedRunnable<?> runnable,
357+
Function<? super Throwable, ? extends L> leftFn) {
361358
return Try.trying(runnable).toEither(leftFn);
362359
}
363360

@@ -366,10 +363,9 @@ public static <T extends Throwable, L> Either<L, Unit> trying(CheckedRunnable<T>
366363
* exception, wrap it in a left value and return it.
367364
*
368365
* @param runnable the runnable
369-
* @param <T> the left parameter type (the most contravariant exception that runnable might throw)
370366
* @return {@link Unit} as a right value, or a left value of the thrown exception
371367
*/
372-
public static <T extends Throwable> Either<T, Unit> trying(CheckedRunnable<T> runnable) {
368+
public static Either<Throwable, Unit> trying(CheckedRunnable<?> runnable) {
373369
return trying(runnable, id());
374370
}
375371

0 commit comments

Comments
 (0)