What's the deal with all the Either cruft?

前端 未结 4 1816
Happy的楠姐
Happy的楠姐 2020-12-23 14:04

The Either class seems useful and the ways of using it are pretty obvious. But then I look at the API documentation and I\'m baffled:

def joinLeft [A1 >:          


        
4条回答
  •  暖寄归人
    2020-12-23 14:34

    Ignoring the joins for now, projections are a mechanism allowing you to use use an Either as a monad. Think of it as extracting either the left or right side into an Option, but without losing the other side

    As always, this probably makes more sense with an example. So imagine you have an Either[Exception, Int] and want to convert the Exception to a String (if present)

    val result = opReturningEither
    val better = result.left map {_.getMessage}
    

    This will map over the left side of result, giving you an Either[String,Int]

提交回复
热议问题