0
votes

I have a function that iterate over a list of a record type:

let resAddr (ranges: IpRanges list) =
    ranges
    |> List.iter (fun e ->
                 {
                    let {ipStart=ipStart;ipEnd=ipEnd;subnet=subnet;gateway=gateway} = e
                    printfn "%O" ipStart
                 })

The compiler complain with error

Invalid record, sequence or computation expression. Sequence expressions should be of the form 'seq { ... }'

What am I doing wrong?

1
Don't put function body after fun e -> in curly braces - Petr
Function body has several lines, how should I then solved the problem? - softshipper
Just use several lines as in answer below - Petr

1 Answers

4
votes

Don't put the function body in braces, F# doesn't use braces for blocks, it uses whitespace. So just make sure to ident your function correctly.

let resAddr (ranges: IpRanges list) =
    ranges
    |> List.iter (fun e ->
                      let { ipStart=ipStart; ipEnd=ipEnd; subnet=subnet; gateway=gateway } = e
                      printfn "%O" ipStart
                 )

Also, I find that when my functions becomes more than one line I usually prefer to declare them separately like this to help readability

let resAddr (ranges: IpRanges list) =
    let internalHandleRange range =
        let { ipStart=ipStart; ipEnd=ipEnd; subnet=subnet; gateway=gateway } = range
        printfn "%O" ipStart
    ranges
    |> List.iter internalHandleRange