I have two records that have a parent-child relationship:
type Parent =
{ Number: int
Child: Child }
and Child =
{ String: string
Parent: Parent }
I have tried initializing these using the following syntax, which doesn't work:
let rec parent =
{ Number = 1
Child = child }
and child =
{ String = "a"
Parent = parent }
this leads to
parent : Parent = { Number = 1
Child = null }
child : Child = { String = "a";
Parent = { Number = 1
Child = null } }
How to I initialize these without relying on mutable fields or copy-and-update after the fact using with?
Here's the syntax to initialize it:
let rec parent =
{ Number = 1
Child =
{ String = "a"
Parent = parent }
}
The result is:
parent : Parent = { Number = 1
Child = { String = "a"
Parent = ... } }
Note that as described in this answer, this syntax may be more accidental than intentional, and will not work for more than simple self-references (e.g. passing the recursive value into a function).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With