Is there a compiler implementation reason why records can t have the AllowNullLiteralAttribute attribute or is this a chosen constraint?
I do see this constraint force cleaner code sometimes but not always.
[<AllowNullLiteralAttribute>]
type IBTreeNode = {
mutable left : IBTreeNode; mutable right : IBTreeNode; mutable value : int}
with
member this.Insert x =
if x < this.value then
if this.left = null then
this.left <- {left = null; right = null; value = x}
else
this.left.Insert x
else
if this.right = null then
this.right <- {left = null; right = null; value = x}
else
this.right.Insert x
// Would be a lot of boilerplate if I wanted these public
[<AllowNullLiteralAttribute>]
type CBTreeNode(value) =
let mutable left = null
let mutable right = null
let mutable value = value
with
member this.Insert x =
if x < value then
if left = null then
left <- CBTreeNode(x)
else
left.Insert x
else
if right = null then
right <- CBTreeNode(x)
else
right.Insert x
Added an immutable version for the frown on mutability crowd. It s about 30% faster in this case.
type OBTree =
| Node of OBTree * OBTree * int
| Null
with
member this.Insert x =
match this with
| Node(left, right, value) when x <= value -> Node(left.Insert x, right, value)
| Node(left, right, value) -> Node(left, right.Insert x, value)
| Null -> Node(Null, Null, x)