无效的记录、序列或计算表达式

Invalid record, sequence or computation expression

我有一个迭代记录类型列表的函数:

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

编译器报错

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

我做错了什么?

不要将函数体放在大括号中,F# 不对块使用大括号,它使用空格。因此,请确保正确识别您的函数。

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

此外,我发现当我的函数多于一行时,我通常更喜欢像这样单独声明它们以提高可读性

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