我已经看过许多使用Option(对于简单值)或者[List [Error],T]来处理错误的scala代码片断。在scala中使用异常是一个糟糕的做法吗?
这给地方像这样的代码
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)
)
}
)
}
)
其产生的不那么漂亮的代码嵌套,迫使你处理故障的每一个步骤,同时也迫使你把所有的函数返回一个要么[ ...]
所以我想知道,如果
异常的使用Scala中(或一般函数式编程)气馁
有使用它们(关于不变性或代码并发)
例外是某种方式与原则或冲突功能的编程
你能想到的一个更好的方式来编写的任何缺点给出的例子
-
人们可以通过退出本功能避免嵌套离子一旦发现使用return语句的错误,但使用返回也不鼓励在标量...
相关http://stackoverflow.com/questions/12886285/throwing-exceptions-in-scala-what-is-the-official-rule的链接 –
感谢... – opensas
平面图,狗屎 –