-
Notifications
You must be signed in to change notification settings - Fork 20
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
How to return values from a result block #5
Comments
The most natural answer is just to return the final line. Not asserting that this is a result tuple allows the following use cases. OK.for do
data <- SignupForm.validate(form)
customer <- MyApp.signup(data)
Response.ok(welcome_page(customer))
rescue
form_with_errors in InvalidForm ->
Response.bad_request(signup_page(form_with_errors))
error in EmailConflict ->
Response.conflict(signup_page(form))
end |
Currently we assert that the last line is a result tuple. I am thinking of dropping this requirement. OK.with do
# stuff
_ <- foo() # foo must return {:ok, v} or {:error, reason}
end OR just have the last line clearly show intent OK.with do
# stuff
x <- foo()
OK.success(x)
end OK.with do
# stuff
foo() # foo can return anything
end |
This would enable easier integration with Phoenix channels that expect a |
What was the work around? @bill-mybiz |
@CrowdHailer I changed my command pattern implementations to all return ok/error tuples and wrapped those in the Before: After: There is also a |
Take the following example, which is available in the test suite
Wrapping the result in an ok tuple and matching it to a throw away variable is noise, that I would like to remove.
Currently replacing the last line with the following alternative will error.
This is probably the correct behaviour, if we allow the above we have to assume the last line is a success. One alternative is to assume the last line always returns a result tuple. Then the last line can be simplified to
A more advanced option would be to add a
yield
block.The problem with this is that yield is not a keyword like else to try/yield/end will not look the same as if/else/end.
Could use else as the start of the yield block, messy if we ever decide to use else for some error handling.
Could see if there is a way to add yield or another keyword to behave the same way as else
The text was updated successfully, but these errors were encountered: