I am currently trying to implement some validation in my F# program but i'm facing som compile time errors, which I can't solve. Boiled down, i'm trying to do this
let ifFalseThenError x errorMessage = function
| true -> Ok x
| false -> Error errorMessage
let val1 (initialeSandssynligheder : float[]) =
initialeSandssynligheder
|> Seq.sum
|> (fun x -> x < 2)
|> (fun x -> ifFalseThenError initialeSandssynligheder "Some error")
let val2 initialeSandssynligheder =
initialeSandssynligheder
|> Array.exists ((<) 0.0)
|> (fun x -> ifFalseThenError initialeSandssynligheder "SomeOtherError")
let beregnAlleOvergangssandsynligheder (initialeSandssynligheder: float[]) : float[] =
let initialeSandssynligheder = [|1.0;1.0;1.0;1.0;1.0|]
let initssh = initialeSandssynligheder
|> val1
|> Result.bind val2 // Error here
initssh
So this my compiler tells me, that I have a type mismatch. I understand, that both val1 and val2 takes a float[] and returns a Result<Float[], String> and I need to compensate for that, when im piping in my val2 and I though I could do that by using Result.bind. What am i misundertanding.
My error:
Error FS0001 Type mismatch. Expecting a
'(bool -> Result<float [],string>) -> 'a'
but given a
'Result<'b,'c> -> Result<'d,'c>'
The type 'bool -> Result<float [],string>' does not match the type 'Result<'a,'b>'... (Path)
So the message is telling my what I though I was compensating for, when adding the Result.bind statement to the last validation