Home

Enumerations

 

Enumerations Fundamentals

 

Introduction

Imagine you have a few constant natural numbers (it doesn't matter what those numbers are, as long as you know them) and you want to use them in a scenario such as a conditional statement. Here is an example:

let membershipCategory = 20;

if membershipCategory = 10 then
    printfn "Membership Category: Teen";
elif membershipCategory = 20 then
    printfn "Membership Category: Adult";
elif membershipCategory = 30 then
    printfn "Membership Category: Senior";

In this case, we used the constants 10, 20, and 30 to perform comparisons. On paper, those numbers would mean different things to different people. Instead of using such numbers, you can name them and use the names. An enumeration is a technique of creating names to be used in place of constant numbers.

Creating an Enumeration

The primary formula to create an enumeration is:

type EnumerationName =
    | MemberName1
    | MemberName2
    | MemberName_n

You start with the required type keyword. This is followed by a name for the enumeration. The name usually starts with an uppercase letter. The name is followed by the = sign followed by the body of the enumeration. Each member of the enumeration starts on its own line with | and a name. Here is an example:

type Category =
    | Teen
    | Adult
    | Senior

Using an Enumeration

As always in F#, you can declare a variable without specifying its type. In the same way concerning enumerations, you can declare a variable and, to indicate that it is bound to an enumeration, initialize the variable using a member of the enumeration. Here is an example:

type Category =
    | Teen
    | Adult
    | Senior
    
let membership = Adult;

In the same way, you can access any member of the enumeration in your code. Here are example:

type Category =
    | Teen
    | Adult
    | Senior
    
let membership = Adult;

if membership = Teen then
    printfn "Membership Category: Teen";
elif membership = Adult then
    printfn "Membership Category: Adult";
elif membership = Senior then
    printfn "Membership Category: Senior";

This would produce:

Membership Category: Adult
Press any key to continue . . .

In the same way, if you are using a matching pattern, you can use the members of the enumeration for each case. Here is an example:

type Category =
    | Teen
    | Adult
    | Senior
    
let membership = Adult;

match membership with
    | Teen -> printfn "Membership Category: Teen";
    | Adult -> printfn "Membership Category: Adult";
    | Senior -> printfn "Membership Category: Senior";

Remember that you can specify the data type of a variable by following its name with a colon and the data type. This can be done as follows for an enumeration:

type Category =
    | Teen
    | Adult
    | Senior
    
let membership : Category = Adult;

Furthermore, to refer to a member of an enumeration and to make your code easier to read, you should (always) qualify each member of an enumeration. This is done by preceding the name of the member of the enumeration with the name of the enumeration separated by a period. Here are examples:

type Category =
    | Teen
    | Adult
    | Senior
    
let membership = Category.Adult;

if membership = Category.Teen then
    printfn "Membership Category: Teen";
elif membership = Category.Adult then
    printfn "Membership Category: Adult";
elif membership = Category.Senior then
    printfn "Membership Category: Senior";
 
   
 

The Values of Members of an Enumeration

An enumeration contains members whose name actually represents a constant value. The numeric value can be anything you want. In fact, you can specify the value of each member as you want. To do this, assign the desired number to each member of the enumeration. The formulat to use is:

type EnumerationName =
    | MemberName1 [ = IntegralValue1 ]
    | MemberName2 [ = IntegralValue2 ] ]
    | MemberName_n [ = IntegralValue_n ]

As you saw in the beginning, you don't have to specify the values of the members of an enumeration; but unlike other languages such as C/C++ or C#, if you decide to give values, you must assign a number to each member of the enumeration. Here are examples:

type Category =
    | Teen = 10
    | Adult = 20
    | Senior = 30
    
let membership = Category.Adult;

if membership = Category.Teen then
    printfn "Membership Category: Teen";
elif membership = Category.Adult then
    printfn "Membership Category: Adult";
elif membership = Category.Senior then
    printfn "Membership Category: Senior";

Remember that you can assign any value you want to each member of the enumeration but the values must be integers or characters of type sbyte, byte, int16, uint16, int32, uint32, int64, uint16, uint64, or char.

 
 
   
 

Previous Copyright © 2009-2015 FunctionX Next