I am familiar with the fact that in F# there is no equivalent "return" keyword.
However we came across an issue recently where we have needed a workflow that consists of many steps, where each step can return a good or bad result. If a bad result is found in any of the steps, we wanted to exit the workflow - and exit early!
We got around it by effectively checking for an Error in each step (i.e. function) but I dont feel that this is the correct way to do it - it is inefficient and we dont exit early.
A sample function in the workflow is as follows:
let StepB stepAResult someParameters =
match stepAResult with
| Good(validResourceData) ->
// Do current step processing
// Return current step result
| Error(error) -> error |> Result.Error
The workflow itself is like the following:
let Workflow someParameters =
let stepAResult = StepA someParameters
let stepBResult = StepB stepAResult someParameters
let stepCResult = StepC stepBResult someParameters
let stepDResult = StepD stepCResult someParameters
stepDResult
So each sample function would take in the result of the previous function and only execute the current step if there was no Error!
The problem I have with this is that if StepA fails with an Error, every other step is still called.
Is there a "functional" way of "returning early" instead of calling every function in the workflow, where we must check each time for an Error?