Want to throw an IOException out of Stream? in that case

^Logo by Use Try or Either from Vavr.

Is there something like this?

image.png

A case where you are processing a value in Stream and you have to handle a checked exception in a lambda expression in the middle

Note: Please think that it is difficult to change the BookReader because it is external.

Common coping

image.png

A case where the runtime error is changed to RuntimeError. If you're handling errors properly within a method or in a private range, it's hard if you're throwing exceptions into the public world. Also, I'm worried that even if I don't do that now, I can casually do that.

You should use Stream forcibly

image.png

Right. I think it's better than throwing a RuntimeError. The number of local variables has increased from 0 to 2, the amount of coding has more than doubled, and the nesting is 2 levels deeper, but if the whole team is satisfied with this, it would be good. ..

Main subject

For example, Golang uses tuples (sets of multiple instances) to propagate anomalous results to the caller, but Java does not support tuples at the language specification level. You can substitute it by creating a class, but for functional programming, you can use Scala or Haskell for a better idea.

Vavr is a library that facilitates functional programming in Java.

Either

A class that takes either a Right or Left value. It is customary to set Right for the normal system and Left for the abnormal system. (pun)

I think it would be nice if you could have an enhanced image of ʻOptional. With ʻOptional, only information about whether or not it is null can be sent downstream of Stream, but with ʻEither`, values such as exceptions can be sent.

Try

It is a class for writing try-catch as a function type as it is. You can receive a lambda expression that can throw a checked exception and express the processing when an exception occurs in the form of lambda.

Think of this as an enhanced version of ʻOptional that contains either normal or abnormal values, just like ʻEither.

Looking only at the atmosphere, it seems that Try is an option, but in Try, the input of abnormal processing becomes Throwable and downcasting tends to occur, so there are cases where writing with ʻEither` is not moyamoya.

I will write

For example, I think it will look like this.

Either: image.png

Try: image.png

Note that Try is throws Throwable. Downcasting is required for proper handling.

By the way

Once you start using Vavr hard, it will no longer look like Java and will be difficult to maintain, and you will start asking yourself why you are not using Scala or Haskell, so it is recommended to use it as a pinch.

Other solutions

Isn't it necessary to continue using Java forcibly? May be advised to the boss.

Miscellaneous feelings

I wonder if this is about OSS ...

Recommended Posts

Want to throw an IOException out of Stream? in that case
Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
I want to send an email in Java.
I want to stop snake case in table definition
I want to write a loop that references an index with Java 8's Stream API
Convert an array that may be null to a stream
Generate Stream from an array of primitive types in Java
The story of Collectors.groupingBy that I want to keep for posterity
[Ruby] I want to put an array in a variable. I want to convert to an array
Summary of problems that I could not log in to firebase
I want to change the value of Attribute in Selenium of Ruby
How to get the length of an audio file in java
[Rails] I want to send data of different models in a form
How to convert an array of Strings to an array of objects with the Stream API
My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
I want to display an error message when registering in the database
I want to ForEach an array with a Lambda expression in Java