I recently found (see near the end of this page) that it's possible to set properties on initialization, as in the last line of the following. This is very concise:
type Account() =
let mutable balance = 0.0
member this.Balance
with get() = balance
and set(value) = balance <- value
let account1 = new Account(Balance = 1543.33)
Is there a way to set sub-properties (i.e. properties of properties) in a similarly concise way, without overwriting them completely?
For example, I would like to write something along these lines:
type Person() =
let mutable name = ""
let mutable someProperty = ""
member this.Name
with get() = name
and set(value) = name <- value
member this.SomeProperty
with get() = someProperty
and set(value) = someProperty <- value
type Account() =
let mutable balance = 0.0
let mutable person = new Person(SomeProperty = "created by an account")
member this.Person
with get() = person
and set(value) = person <- value
member this.Balance
with get() = balance
and set(value) = balance <- value
let account1 = new Account(Balance = 1543.33, Person.Name = "John Smith")
However, the last line produces a compile error which doesn't make complete sense: Named arguments must appear after all other arguments
.
Please note this is actually for interop with a C# library, so I can't necessarily construct a new object for the property. I wouldn't use mutable properties like this in F# if at all possible.