Is the use of exceptions a bad practice in scala?

前端 未结 3 626
鱼传尺愫
鱼传尺愫 2021-02-02 11:04

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

         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-02 11:13

    As om-nom-nom said, I asked a similar question: Throwing exceptions in Scala, what is the "official rule"

    But it's not the only one I asked that may interest you, because I used to code with a lot of boilerplate code and a lot of indentation levels because of pattern matching etc...


    • You can check these links: Handling failures with Either -> Where is the stacktrace?

    • Kind of related to error handling too, which may interest you: Method parameters validation in Scala, with for comprehension and monads In which Travis Brown gave a more detailed answer, about using applicative functors and Scalaz to do fail-fast (first error blocks the process) or collection all errors of a suite of operations Same kind of question: Handling fail-fast failures when the return type is Option[Error]

    • And you can check this link which uses by-name parameters do perform of sequence of operations too. It may be a good alternative to using right projections in a for comprehension, but you can't create intermediare results :( Validation with a sequence of by-name parameters in Scala? I don't know if it can be used with your code exemple but I don't think so in this case (assuming your refreshApplicationToken is free of side effects and return a newly created immutable user instance, instead of mutating a token variable)

提交回复
热议问题