vavr-io / vavr

vʌvr (formerly called Javaslang) is a non-commercial, non-profit object-functional library that runs with Java 8+. It aims to reduce the lines of code and increase code quality.
https://vavr.io
Other
5.68k stars 631 forks source link

Add support for threaded use of Try.withResources() for up to 3 arguments #2752

Open thinstripe opened 10 months ago

thinstripe commented 10 months ago

It would be useful to be able to thread your Autocloseable resources when using Try.withResources() in common scenarios/use cases such as database related resource management (Connection, PreparedStatement, ResultSet).

For example :-

Try.withResources(dataSource::getConnection, conn -> conn.prepareStatement("SELECT id, name from books"), PreparedStatement::executeQuery).of(rs -> /* extract what you need from ResultSet */)

Is equivalent but preferable to :-

Try.of(() -> try(Connection conn = dataSource.getConnection(); PreparedStatement stmt = conn.prepareStatement("SELECT id, name from books"); ResultSet rs = stmt.executeQuery())).flatMap(rs -> /* extract what you need from ResultSet */);