在Scala中使用例外是不好的做法吗? [英] Is the use of exceptions a bad practice in scala?

查看:113
本文介绍了在Scala中使用例外是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过很多次scala代码使用Option(简单的值)或者[List [Error],T]来处理错误。



这个给出这样的代码

  def createApplicationToken(accessToken:AccessToken):[列表[错误],ApplicationToken] = {

//转到社交信息提供者并获取信息
retrieveProviderInfo(accessToken).fold(
errors => Left(errors),
info => {
//尝试使用提供者
//中的信息查找用户//如果不存在,创建用户
User.findOrCreateFromProviderInfo(info).fold(
errors => left(errors),
user => {
//尝试创建一个新的令牌并将其保存到用户
user.refreshApplicationToken.fold(
errors =>左(错误),
user => Right(user.token)

}

}

哪个p导致一个不太好的代码嵌套,强制你处理每一步的失败,并强制你让所有的功能返回一个[...]



所以我想知道如果




  • 在scala(或一般的功能编程)中不鼓励使用异常。 p>


  • 使用它们有任何缺点(关于不可变性或代码并发)


  • 某些方面与原则或功能规划相冲突


  • 你可以想到一个更好的方式来编写给定的例子




-



一旦发现错误,就可以通过退出函数来避免嵌套返回语句,但是在scala中使用return也是不鼓励的。

解决方案

以下版本使用正确的投影的 是一个monad,完全相当于您的代码:

  def createApplicationToken(accessToken:AccessToken)= for {
info< - retrieveProviderInfo(accessToken).right
user< ; - User.findOrCreateFromProviderInfo(info).right
refr< - user.refreshApplicationToken.right
} yield refr.token

而且,更好地展现了的优点。



更一般来说,规则与Java中的规则相同:对异常情况使用异常。您可能会发现,当您以这种风格工作时,您会更改您异常的定义 - 例如,无效的用户输入并不是特别的,超时的网络请求不是真的例外等。


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...

解决方案

The following version uses the fact that the right projection of Either is a monad, and is exactly equivalent to your code:

def createApplicationToken(accessToken: AccessToken) = for {
   info <- retrieveProviderInfo(accessToken).right
   user <- User.findOrCreateFromProviderInfo(info).right
   refr <- user.refreshApplicationToken.right
} yield refr.token

And does a much better job of showing off the advantages of Either.

More generally, the rules are the same as in Java: use exceptions for exceptional situations. You just might find that you change your definition of exceptional a bit when you're working in this style—e.g., invalid user input isn't really exceptional, a timed-out network request isn't really exceptional, etc.

这篇关于在Scala中使用例外是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆