'Mutable internal value in discriminated union

The task is to create a binary tree with its nodes having a member is_locked that can be true or false and a method lock() that shall set is_locked to true if it's not already.

I tried this

type BinaryTreeNode =
    | Node of BinaryTreeNode * BinaryTreeNode
    | End
    with
        let mutable internalIsLocked = false
        member this.is_locked
            with get() = internalIsLocked 
        member this.lock() = internalIsLocked <- true

but compiler says no due to This declaration element is not permitted in an augmentation at the let binding.

val mutable internalIsLocked : bool instead does not work for the same reason.

How can this problem be solved then? Is it not solvable with discriminated unions?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source