Last active
October 21, 2016 23:41
-
-
Save reidev275/2f318758931a5f40aa83 to your computer and use it in GitHub Desktop.
ROP Result Discriminated Union interop with C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the two-track type | |
type Result<'TSuccess,'TFailure> = | |
| Success of 'TSuccess | |
| Failure of 'TFailure | |
type ResultInterop<'TSuccess, 'TFailure> = { | |
IsSuccess : bool | |
Success : 'TSuccess | |
Failure : 'TFailure | |
} | |
let toResultInterop result = | |
match result with | |
| Success s -> { IsSuccess=true; Success=s; Failure=Unchecked.defaultof<_> } | |
| Failure f -> { IsSuccess=false; Success=Unchecked.defaultof<_>; Failure=f } | |
module MyFSharpModule = | |
let validate request = | |
if request.isValid then | |
Success "Woot" | |
else | |
Failure "request not valid" | |
let handleUpdateRequest request = | |
request | |
|> validate | |
|> toResultInterop | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public string Get(Request request) | |
{ | |
var result = MyFSharpModule.handleUpdateRequest(request); | |
if (result.IsSuccess) | |
return result.Success; | |
else | |
throw new Exception(result.Failure); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment