scala - Returning the same value type, but with success/failure -
let's have method..
def foo(b: bar): try[bar] = ???
try
placeholder here. foo
bar
, returns value indicate success/failure. want return original value success/failure indication, when have collection, can know which ones failed , succeeded, , them. try
doesn't work me, because failure
wraps exception (let's don't care reason why failed).
i maybe return either[bar, bar]
, seems redundant repeat type parameter.
are there better alternatives this?
either[bar, bar]
, (boolean, bar)
isomorphic , choice between them matter of taste.
i'd prefer either
because nicer set of operations mapping on collection pattern matching, etc., merge
extension method allows write results.map(_.merge)
seq[bar]
if in situation no longer need make distinction between successful , failed results. find this:
val result: either[bar, bar] = foo(input).tooption.toright(input)
a little nicer than:
val result: (boolean, bar) = foo(input).map((true, _)).getorelse((false, input))
or alternatives, mileage may vary.
Comments
Post a Comment