One way to model copy-and-update expressions for classes is with a copy constructor that accepts optional arguments.
type Person(first, last, age) = new (prototype: Person, ?first, ?last, ?age) = Person(defaultArg first prototype.First, defaultArg last prototype.Last, defaultArg age prototype.Age) member val First = first member val Last = last member val Age = age let john = Person("John", "Doe", 45) let jane = Person(john, first="Jane")
EDIT
You did not ask for it, but in many cases you make the results with the mutable class more clear code:
type Person(first, last, age) = member val First = first with get, set member val Last = last with get, set member val Age = age with get, set member this.Clone() = this.MemberwiseClone() :?> Person let john = Person("John", "Doe", 45) let jane = john.Clone() in jane.First <- "Jane"
Daniel
source share