2
如果我有一個或多個Either值,我怎樣才能以功能方式編寫它們,這樣如果存在左值,我可以記錄錯誤,但如果存在正確的值, 。例如如何在功能上編寫Scala的任一個
def composeEither(ethr: Either[Error, String]): Either[Error, String] = {
ethr match { case Left(err) => log.error(err.getMessage) }
//this obviously will not work but something like map that is right biased
ethr map { e => e } //Just for the sake of example - there might be a different either calculated within the map
}
讓我知道是否還有其他任何我可以添加,使我的問題更清晰。
你應該使用'Try',而不是'Either' – Dima
我使用的返回要麼,我想一個第三方庫通過不保持代碼乾淨任意轉換,如果我不必。 – goodOldFashioned
你在斯卡拉2.11(無論是無論是)或2.12(右偏哪一個)? –