Como recupero um valor de um tipo genérico composto?
Como recupero um valor de um genérico?
Especificamente, estou tentando o seguinte:
// Test
let result = Validate goodInput;;
// How to access record??
let request = getRequest result
Aqui está o código:
type Result<'TSuccess,'TFailure> =
| Success of 'TSuccess
| Failure of 'TFailure
let bind nextFunction lastFunctionResult =
match lastFunctionResult with
| Success input -> nextFunction input
| Failure f -> Failure f
type Request = {name:string; email:string}
let validate1 input =
if input.name = "" then Failure "Name must not be blank"
else Success input
let validate2 input =
if input.name.Length > 50 then Failure "Name must not be longer than 50 chars"
else Success input
let validate3 input =
if input.email = "" then Failure "Email must not be blank"
else Success input;;
let Validate =
validate1
>> bind validate2
>> bind validate3;;
// Setup
let goodInput = {name="Alice"; email="abc@abc.com"}
let badInput = {name=""; email="abc@abc.com"};;
// I have no clue how to do this...
let getRequest = function
| "Alice", "abc@abc.com" -> {name="Scott"; email="xyz@xyz.com"}
| _ -> {name="none"; email="none"}
// Test
let result = Validate goodInput;;
// How to access record??
let request = getRequest result
printfn "%A" result