Vavr: Try variant for code producing side-effects only (i.e. returning void)

Created on 1 Dec 2014  路  2Comments  路  Source: vavr-io/vavr

Try needs a factory method to accept units of work returning nothing, i.e. void.

Try.of(CheckedSupplier) and Try.of(CheckedRunnable) would be ambiguous regarding Try.of(() -> { throw new RuntimeException(); }).

Therefore the new factory method is named Try.run(CheckedRunnable).

The return value of Try.run(...) is Try<Void>. In the case of a Success<Void>, the get() will return null, signaling the absence of a value.

We could introduce a new type Try.Nothing extends Try.Success<Void> - but I currently not see any benefits. The result of a 'Try' can be either a success or a failure but not nothing at all.

feature

Most helpful comment

@zerkowsm Try.run(SomeClassWithVoidMethodThrowingAnException::method) is your friend

All 2 comments

So, is there a way to have Try.of with void methods - something like:
Try.of(() -> SomeClassWithVoidMethodThrowingAnException::method);
or explicitly:
`Try.of(() -> SomeClassWithVoidMethodThrowingAnException::method).get();?

@zerkowsm Try.run(SomeClassWithVoidMethodThrowingAnException::method) is your friend

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Pyeroh picture Pyeroh  路  3Comments

carnott-snap picture carnott-snap  路  4Comments

HiDAl picture HiDAl  路  3Comments

yarulan picture yarulan  路  5Comments

roookeee picture roookeee  路  5Comments