Professional Documents
Culture Documents
En - Chapter 2 - General Discussion
En - Chapter 2 - General Discussion
General discussion
Constructs
Records Members Generic records Units of measure
Records (*)
Complex data structure
Position x, position y, mass, etc Type
float*float*float*...*float
Records (*)
type RECORD_NAME = { Field1Name : Field1Type; Field2Name : Field2Type; ... FieldnName : FieldnType }
Records (*)
Example record
type Person = { Name : string; Surname : string; Age : int }
Records (*)
Reading the fields of a record
let name = john_doe.Name
Type inference
let fullname x = x.Name+","+x.Surname
Records (*)
Modifying a record
{ x with FieldiName = vi; ... FieldjName = vj }
Example
let jane_doe = { john_doe with Name = "Jane" }
Members (*)
Member functions
Invoked with dot operator Increased capabilities of manipulation Indentation is very important
Members (*)
Example
type Employee = { Person : Person Job : string } member self.ChangeJob new_job = { self with Job = new_job }
Members (*)
Properties
Parameterless members
type Department = { Boss : Person NumEmployees : int Name : string } member this.Description = "Department "+this.Name+" is managed by "+this.Boss.Name
Members (*)
Static members
type Job = { Department : string HoursPerWeek : int SalaryPerWeek : int } static member CreateDefaultJob() = { Department = "Accounting"; HoursPerWeek = 40; SalaryPerWeek = 400 }
Members (*)
Mutable fields
type Traveler = { Person : Person mutable Location : string CurrentActivity : Ref<string> } let john_traveler = { Person = john_doe; Location = "Cambodia"; CurrentActivity = ref "Fishing" } do john_traveler.Location <- "California" do john_traveler.CurrentActivity := "Surfing"
RECORD-TYPE-PARAMS =
'a,'b,'c ,
...
let float_num = { Zero = 0.0; One Mul = (*); Neg = let int_num = { Zero = 0; One = Mul = (*); Neg =
let float_line = line float_num let int_line = line int_num let line_3x2 = int_line 2 3 let line_4x2 = float_line 2.0 4.0
Error
x+g
Ok
let v4 = { X = v1.X/v2.X; Y = v2.Y/v1.Y }
Compiler error
Public messages
Through any channel