I've seen many times pieces of scala code using Option (for simple values) or Either[List[Error], T] for handling errors.
this gives place to code like this
def createApplicationToken(accessToken: AccessToken): Either[List[Error], ApplicationToken] = {
// go to social info provider and fetch information
retrieveProviderInfo(accessToken).fold(
errors => Left(errors),
info => {
// try to find user using the info from the provider
// if it's not there, create user
User.findOrCreateFromProviderInfo(info).fold(
errors => Left(errors),
user => {
// try to create a fresh token and save it to the user
user.refreshApplicationToken.fold(
errors => Left(errors),
user => Right(user.token)
)
}
)
}
)
Which produces a not so nice code nesting, forces you to deal with failures on every step, and also forces you to have all your functions return a Either[...]
So I'd like to know if
the use of exceptions is discouraged in scala (or functional programming in general)
there are any drawbacks in using them (regarding immutability or code concurrency)
exceptions are somehow in conflict with the principles or functional programming
you can think of a better way to code the given example
--
One could avoid the nesting by exiting the function as soon as an error is found using the return statement, but using return is also discouraged in scala...