I'm trying to get a function working which unwraps an envelope type, applies a function to the content and returns an envelope type. Sort of like the bind function of a burrito.
type Envelope<'a> = { Content : 'a ; Errors : string list } let (!>) f e = let {Content=content:'a; Errors=errors} = e match errors with | [] -> e : Envelope<'a> | _ -> f content : Envelope<'b> The error is:
This construct causes code to be less generic than indicated by the type annotations. The type variable 'a has been constrained to be type ''b'.
I have a "feeling" why it's wrong, sometimes I'm returning an Envelope<'a> and the other times I'm returning an Envelope<'b>.
How can I get this to work? I'm trying to make it "work" like I would a bind function on, for example, an Option type:
let (>>=) f o = match o with | Some v -> f v | None -> None
Result<'TSuccess, 'TError>type (built in to F# since F# 4.1). See github.com/fsharp/fslang-design/blob/master/FSharp-4.1/… for more details. In your case, I think'TErrorwould bestring list.Option.defaultWith.