I'm designing a method returning and want to keep referntial transparency. So instead of returning the result and checking preconditions I return Either[Throwable, String]
. It currently looks as follows:
def logName(arg: String): Either[Throwable, String] =
if (arg.lenght < 5)
Right(arg)
else
Left(new IllegalArgumentException(s"Illegal lenght"))
The thing I was confused by is that I create exception and don't throw it, but just store it inside Left
object. Is that a common pattern to preserver referential transparency?