I have a record type:
type Person =
{
Name : string
Age : int
}
And I need a function which returns new instances of this record with default (in C# point of view) fields.
I can do it like this:
let f =
{
Name = ""
Age = 0
}
but I would like to avoid all fields enumeration and use something like this:
let f = new Person
Is there any way to do it ?
Used unchecked.DefaultOf<'t>
. However be aware that F# records are immutable, so this is probably not what you want.
Hmm... defaultof<Person> returns null. It is not answer to this question, but it is excellent solution for this stackoverflow.com/questions/28790446/… my related problem ! Thanks
I think there might be a bit of the X-Y problem here. You really don't want to be using records in the situation that they can be empty as they are immutable