-
Notifications
You must be signed in to change notification settings - Fork 9
Records and types
LENS provides two ways of defining a custom data structure inside the script: via records and types.
A record contains several named fields of different types:
record Cat
Name : string
Age : int
IsCute : bool
Instances of records are created using the new
keyword. Records always have two constructors defined: the default one and the one that initializes all fields in specified order.
let madTom = new Cat ()
let mrWhiskers = new Cat "Mr Whiskers" 2 true
let tardar = new Cat "Tardar Sauce" 1 false
Records also get GetHashCode
and equality comparison methods for free!
A type in LENS defines a list of possible values. It is similar to the enum
construct from C#, but type labels in LENS may have tags associated with them:
type Suit
Hearts
Clubs
Spades
Diamonds
type Card
Ace of Suit
King of Suit
Queen of Suit
Jack of Suit
ValueCard of Tuple<Suit, int>
If the label does not have a tag, its name is declared as a global read-only property. Otherwise a global eponymous function is created:
let sp = Spades
let ace = Ace Spades
let ten = ValueCard new (Clubs; 10)
Both records and types are pass-by-reference in .NET terms, deriving from System.Object
. There is no way to define a custom value type in a LENS script.