Getting Value of Either

前端 未结 4 1170
长情又很酷
长情又很酷 2021-01-01 11:54

Besides using match, is there an Option-like way to getOrElse the actual content of the Right or Left value?



        
4条回答
  •  遥遥无期
    2021-01-01 12:16

    Given type A on both sides, that is, Either[A, A], we can use Either.merge

    ...to extract values from Either instances regardless of whether they are Left or Right.

    Note if left and right types differ then result becomes Any:

    val e: Either[Int, String] = Right("hello")
    e.merge // hello: Any
    

提交回复
热议问题