如何在F#中初始化相互递归记录
How to initialize mutually recursive records in F#
我有两条具有父子关系的记录:
type Parent =
{ Number: int
Child: Child }
and Child =
{ String: string
Parent: Parent }
我试过使用以下语法初始化这些,但不起作用:
let rec parent =
{ Number = 1
Child = child }
and child =
{ String = "a"
Parent = parent }
这导致
parent : Parent = { Number = 1
Child = null }
child : Child = { String = "a";
Parent = { Number = 1
Child = null } }
如何在不依赖可变字段或事后使用 with
进行复制和更新的情况下初始化这些?
初始化它的语法如下:
let rec parent =
{ Number = 1
Child =
{ String = "a"
Parent = parent }
}
结果是:
parent : Parent = { Number = 1
Child = { String = "a"
Parent = ... } }
请注意 ,此语法可能更多的是偶然而非故意的,并且仅适用于简单的 self-references(例如将递归值传递给函数)。
我有两条具有父子关系的记录:
type Parent =
{ Number: int
Child: Child }
and Child =
{ String: string
Parent: Parent }
我试过使用以下语法初始化这些,但不起作用:
let rec parent =
{ Number = 1
Child = child }
and child =
{ String = "a"
Parent = parent }
这导致
parent : Parent = { Number = 1
Child = null }
child : Child = { String = "a";
Parent = { Number = 1
Child = null } }
如何在不依赖可变字段或事后使用 with
进行复制和更新的情况下初始化这些?
初始化它的语法如下:
let rec parent =
{ Number = 1
Child =
{ String = "a"
Parent = parent }
}
结果是:
parent : Parent = { Number = 1
Child = { String = "a"
Parent = ... } }
请注意