When I have this code:
type HtmlNode =
| HtmlElement of name:string * attribute:HtmlAttribute list
| HtmlText of content:string
and HtmlAttribute =
| HtmlAttribute of name:string * value:string * parent:HtmlNode
let createElement name attrs =
let toAttributes element = [ for name, value in attrs -> HtmlAttribute(name, value, element)]
let rec element = HtmlElement(name, attributes)
and attributes = toAttributes element
element
The compiler gives the following error:
Recursive values cannot appear directly as a construction of the type 'HtmlNode' within a recursive binding. This feature has been removed from the F# language. Consider using a record instead.
Why is that? The let rec is supposed to support the creation of recursive values, and something similar works with records.
I don't know why this was changed, but one workaround is to use seq
instead of list
.
type HtmlNode =
| HtmlElement of name:string * attribute:HtmlAttribute seq
| HtmlText of content:string
and HtmlAttribute =
| HtmlAttribute of name:string * value:string * parent:HtmlNode
let createElement name attrs =
let rec element = HtmlElement(name, attributes)
and attributes = seq { for name, value in attrs -> HtmlAttribute(name, value, element) }
element
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