Search code examples
lambdaf#record

Creating a record inside a lambda


I'm having some troubles with transforming an array inside of an array to a record type. The offending code is this

let arrayToTransaction arr =
        arr 
        |> Array.map (fun x -> splitStr [|"\n"|])
        |> Array.collect 
            (fun x -> { 
                        date = DateTime.Parse(x.[1]);
                        payee = x.[0].Substring(0, x.[0].IndexOf(','))
                        category = "Test category"
                        memo = "Parsed with my F# parser"
                        outflow = Single.Parse(str.Substring(str.IndexOf('-') + 1))
                        inflow = 0.0f
                      })

The error I'm getting is

Error 1 The type ''a []' does not contain a field 'date'

But I have defined the record type correctly I know because this:

let x = { date = DateTime.Now; payee = "test"; category = "test"; memo = "test"; outflow = 0.0f; inflow = 0.0f }

Works just fine.


Solution

  • The function passed to Array.collect should return an array and not a record. You can either wrap your record in an array with [| ... |] or use Array.map instead of Array.collect.