TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to F#

F# Record Examples: Type, With Keywords

Create records with named fields, using the type and with keywords.
Records. A record is a tuple with named fields. In complex programs, having names for items is helpful. It helps us understand what a program is doing.
With simple syntax, we create records by labeling field names and types. A record is created automatically based on the fields we specify. We can copy records while changing these fields.
Primary example. Let us begin with this example. We use the type keyword to create a record type. We specify Name, Weight and Wings on the record. These are of types string, int and bool.

Let: In the let statement we create a new record instance. We designate a cat with weight 12 and no wings.

With: In the next let statement, we create a dog. We copy the cat and change the name to "dog."

Finally: We create a bird record by modifying both the Name and the Wings fields in a previous record.

F# program that uses records type Animal = { Name:string; Weight:int; Wings:bool } // Create an instance of Animal named cat. let cat = { Name="cat"; Weight=12; Wings=false } // Display the cat record. printfn "%A" cat // Display the Name, Weight and Wings properties. printfn "%A" cat.Name printfn "%A" cat.Weight printfn "%A" cat.Wings // Modify an existing record. // ... Set name to "dog." let dog = { cat with Name="dog" } printfn "%A" dog let bird = { cat with Name="bird"; Wings=true } printfn "%A" bird Output {Name = "cat"; Weight = 12; Wings = false;} "cat" 12 false {Name = "dog"; Weight = 12; Wings = false;} {Name = "bird"; Weight = 12; Wings = true;}
Not mutable. Fields in a record cannot be assigned. The F# compiler will report an error saying "This field is not mutable." We must use "with" to copy records, changing values in that way.
F# program that shows mutable error type ExampleRecord = { size:int; valid:bool } let example = { size=10; valid=true } // This causes an error: cannot be compiled. example.size <- 20 Output error FS0005: This field is not mutable
A review. Records have features of classes, like named fields, but are more similar to tuples. Their fields are immutable. And they provide special syntax for modification during copying.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf