Is there an equivalent of Scala's Either in Java 8?

后端 未结 8 794
南旧
南旧 2020-11-28 23:00

Just like java.util.Optional in Java 8 is (somewhat) equivalent to Scala\'s Option[T] type, is there an equivalent to Scala\'s Eithe

8条回答
  •  余生分开走
    2020-11-28 23:48

    cyclops-react has a 'right' biased either implementation called Xor.

     Xor.primary("hello")
        .map(s->s+" world")
    
     //Primary["hello world"]
    
     Xor.secondary("hello")
        .map(s->s+" world")
    
     //Secondary["hello"]
    
     Xor.secondary("hello")
        .swap()
        .map(s->s+" world")
    
     //Primary["hello world"]
    
    Xor.accumulateSecondary(ListX.of(Xor.secondary("failed1"),
                                     Xor.secondary("failed2"),
                                     Xor.primary("success")),
                                     Semigroups.stringConcat)
    
    //failed1failed2
    

    There is also a related type Ior which can act as an either or a tuple2.

    • disclosure I am the author of cyclops-react.

提交回复
热议问题