Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create record instance with default values

Tags:

f#

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 ?

like image 222
ceth Avatar asked Mar 01 '15 05:03

ceth


People also ask

Which request gets the default values to create a record?

Get the default values for fields for a new record of a specified object and optional record type. After getting the default values, make a request to POST /ui-api/records to create the record.

What is default value database?

What Does Default Values - Database Mean? Default values, in the context of databases, are preset values defined for a column type. Default values are used when many records hold similar data.

What is default value salesforce?

Default field values automatically insert the value of a custom field when a new record is created. You can use a default value on a formula for some types of fields or exact values, such as Checked or Unchecked for checkbox fields. After you have defined default values: The user chooses to create a new record.


1 Answers

F# allows adding methods/properties to records and discriminated unions:

type Person =
{
    Name: string
    Age: int
}
with
    static member Default = { Name = ""; Age = 0 }

In FSI:

 > Person.Default;;
    val it : Person = {Name = "";
                       Age = 0;}
like image 97
Eugene Fotin Avatar answered Sep 30 '22 20:09

Eugene Fotin