I'm trying to do some mathematical operations with decimal options on a custom type:
type LineItem = {Cost: decimal option; Price: decimal option; Qty: decimal option} let discount = 0.25M let createItem (c, p, q) = {Cost = c; Price = p; Qty = q} let items = [ (Some 1M , None , Some 1M) (Some 3M , Some 2.0M , None) (Some 5M , Some 3.0M , Some 5M) (None , Some 1.0M , Some 2M) (Some 11M , Some 2.0M , None) ] |> List.map createItem I can do some very simple arithmetic with
items |> Seq.map (fun line -> line.Price |> Option.map (fun x -> discount * x)) which gives me
val it : seq<decimal option> = seq [null; Some 0.500M; Some 0.750M; Some 0.250M; ...] If I try to actually calculate the thing I need
items |> Seq.map (fun line -> line.Price |> Option.map (fun x -> discount * x) |> Option.map (fun x -> x - (line.Cost |> Option.map (fun x -> x))) |> Option.map (fun x -> x * (line.Qty |> Option.map (fun x -> x)))) I get the error
error FS0001: Type constraint mismatch. The type 'a option is not compatible with type decimal The type ''a option' is not compatible with the type 'decimal' where I would have expected a seq<decimal option>.
I must be missing something but I can't seem to spot whatever it is I'm missing.