F# Enumerations

F# Enumerations are sets of value constants that can be named in different ways.

F# Enumeration is one of the special types of data, which represents a fixed type of set of named values that are stored within.

A set of enumerations can be thought of as an easy and concise way to map and manipulate sets of values with a clearly defined and limited domain by defining and working with them.

You will learn about the various ways in which F# enumerations can be utilized to improve the readability, maintainability, and type safety of your your code. By leveraging the benefits of enumerations, you can write more robust and error-free code that is easier to understand and maintain.



Define F# Enumerations

An enumeration in F# is defined using the type keyword followed by the name of the enumeration and the values it contains.

The values of an enumeration are defined using the | operator and are separated by semicolons.

An enumeration can be declared in the following manner:

type enum-name =
| value1 = integer-literal1
| value2 = integer-literal2
...

The following example illustrates how enumerations can be used to represent data:

Example: 

// Declaration of an enumeration. type DayOfWeek = | Mon | Tues | Wed | Thurs | Fri | Sat | Sunlet printDayOfWeek (day: DayOfWeek) = match day with | Mon -> "Monday" | Tues -> "Tuesday" | Wed -> "Wednesday" | Thurs -> "Thursday" | Fri -> "Friday" | Sat -> "Saturday" | Sun -> "Sunday"// Use of an enumeration.let weekend1 : DayOfWeek = DayOfWeek.Sat let weekend2 : DayOfWeek = DayOfWeek.Sun let weekDay1 : DayOfWeek = DayOfWeek.Mon let weekDay2 : DayOfWeek = DayOfWeek.Tues let weekDay3 : DayOfWeek = DayOfWeek.Wed let weekDay4 : DayOfWeek = DayOfWeek.Thurs let weekDay5 : DayOfWeek = DayOfWeek.Friprintfn "Saturday: %A" weekend1 printfn "Sunday: %A" weekend2 printfn "Monday: %A" weekDay1 printfn "Tuesday: %A" weekDay2 printfn "Wednesday: %A" weekDay3 printfn "Thursday: %A" weekDay4 printfn "Friday: %A" weekDay5

The output of the above example will be as follows:

Saturday: Sat
Sunday: Sun
Monday: Mon
Tuesday: Tues
Wednesday: Wed
Thursday: Thurs
Friday: Fri

Below is another example to give a clear understanding of enumeration:

Example: 

// Define an enumeration for different types of animals type Animal = | Dog | Cat | Elephant | Giraffe | Lion | Tiger | Bear// Function that takes an Animal value and returns a string representation let printAnimal (animal: Animal) = match animal with | Dog -> "Dog" | Cat -> "Cat" | Elephant -> "Elephant" | Giraffe -> "Giraffe" | Lion -> "Lion" | Tiger -> "Tiger" | Bear -> "Bear"// Function that checks if an animal is a mammal or not let isMammal (animal: Animal) = match animal with | Dog | Cat | Elephant | Giraffe -> true | Lion | Tiger | Bear -> false// Usage of the Animal enumeration let myPet = Dog let myFavoriteAnimal = Elephant let bigAnimal = Giraffeprintfn "My pet is a %s." (printAnimal myPet) printfn "My favorite animal is an %s." (printAnimal myFavoriteAnimal) printfn "Biggest animal is a %s." (printAnimal bigAnimal)if isMammal myPet then printfn "My pet is a mammal." else printfn "My pet is not a mammal."

And the output will be:

My pet is a Dog.
My favorite animal is an Elephant.
Biggest animal is a Giraffe.
My pet is a mammal.

Example Explanation

We have defined an enumeration called Animal that represents different types of animals, including Dog, Cat, Elephant, Giraffe, Lion, Tiger, and Bear. We also have two functions: printAnimal which takes an Animal value as input and returns a string representation of the animal, and isMammal which checks if an animal is a mammal or not.

We then use the Animal enumeration in our code by creating three variables: myPet which represents my pet and is set to Dog, myFavoriteAnimal which represents my favorite animal and is set to Elephant, and bigAnimal which represents the biggest animal and is set to Giraffe. We use the printAnimal function to print out the string representation of these animals.

Finally, we use the isMammal function to check if myPet is a mammal or not, and print the corresponding message. Overall, the enumeration and functions provide a concise and type-safe way to represent and work with different types of animals in our code.

You can help others learn about the power and flexible nature of F# if conditions by sharing our article on social media below. This will enable them to create dynamic and interactive web applications.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *