Provide monadic conversions for StatusReply#32238
Conversation
Sometimes you want to work with a StatusReply in the context of monadic processing with Try, Either or Option, rather than always pattern matching on it. This provides convenience methods to do that.
| /** | ||
| * Return this StatusReply as a Try. | ||
| */ | ||
| def toTry: Try[T] = status |
There was a problem hiding this comment.
No problem with adding those. Normally StatusReply is supposed to be used with askWithStatus and the returned Future is already flattened to Future[T]. I guess you are using it without askWithStatus?
There was a problem hiding this comment.
Well, I was, I was refactoring some code that was using it, and found myself wanting to convert to either/try. But then as I expanded the scope of the refactoring, I found there was no good reason for it to be using a Future[StatusReply[T]] in the first place, that whatever errors it had were eventually flattened into a Future anyway, so I modified it to use askWithStatus. So, I don't actually have a use case for this now.
I think I would have more of a use case for map and flatMap on it, in the actor behaviour, the code I was working with was passing a lot of StatusReply types around when generating a reply, and although I didn't need these methods, I could see that they might be potentially useful. Though maybe the actor should have just worked with Try. But anyway, thats where toTrymight be useful, to do amapbefore converting back toStatusReply`.
There was a problem hiding this comment.
Then I suggest that we make this minimal by only adding toTry.
Java should already be covered by existing methods.
| /** | ||
| * Return this StatusReply as an Option, returning None and discarding the error if it's an Error. | ||
| */ | ||
| def toOption: Option[T] = status.toOption |
There was a problem hiding this comment.
Add Scala API: to the docs of all 3 methods:
/**
* Scala API: Return this StatusReply as an Option, returning None and discarding the error if it's an Error.
*/
Also add a def toOptional for Java.
Sometimes you want to work with a StatusReply in the context of monadic processing with Try, Either or Option, rather than always pattern matching on it. This provides convenience methods to do that.