我有两条有父子关系的记录:
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
之后依赖可变字段或复制并更新的情况下初始化这些字段?
答案 0 :(得分:3)
以下是初始化它的语法:
let rec parent =
{ Number = 1
Child =
{ String = "a"
Parent = parent }
}
结果是:
parent : Parent = { Number = 1
Child = { String = "a"
Parent = ... } }
请注意as described in this answer,这种语法可能比意图更偶然,并且不仅仅用于简单的自引用(例如将递归值传递给函数)。