Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide monadic conversions for StatusReply #32238

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jroper
Copy link
Contributor

@jroper jroper commented Nov 23, 2023

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.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants