Skip to content

Commit 35da196

Browse files
authored
Enum F# snippets (#7696)
1 parent 1299c41 commit 35da196

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1220
-6
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//<snippet1>
2+
type VehicleDoors =
3+
| Motorbike = 0
4+
| Sportscar = 2
5+
| Sedan = 4
6+
| Hatchback = 5
7+
8+
let myVeh = VehicleDoors.Sportscar
9+
let yourVeh = VehicleDoors.Motorbike
10+
let otherVeh = VehicleDoors.Sedan
11+
12+
printfn $"Does a {myVeh} have more doors than a {yourVeh}?"
13+
printfn $"""{if myVeh.CompareTo yourVeh > 0 then "Yes" else "No"}\n"""
14+
15+
printfn $"Does a {myVeh} have more doors than a {otherVeh}?"
16+
printfn $"""{if myVeh.CompareTo otherVeh > 0 then "Yes" else "No"}"""
17+
18+
// The example displays the following output:
19+
// Does a Sportscar have more doors than a Motorbike?
20+
// Yes
21+
//
22+
// Does a Sportscar have more doors than a Sedan?
23+
// No
24+
//</snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="EnumCompareTo.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module EnumEquals
2+
3+
//<snippet1>
4+
open System
5+
6+
type Colors =
7+
| Red = 0
8+
| Green = 1
9+
| Blue = 2
10+
| Yellow = 3
11+
12+
type Mammals =
13+
| Cat = 0
14+
| Dog = 1
15+
| Horse = 2
16+
| Dolphin = 3
17+
18+
let myPet = Mammals.Cat
19+
let myColor = Colors.Red
20+
let yourPet = Mammals.Dog
21+
let yourColor = Colors.Red
22+
23+
printfn
24+
$"""My favorite animal is a {myPet}
25+
Your favorite animal is a {yourPet}
26+
Do we like the same animal? {if myPet.Equals yourPet then "Yes" else "No"}
27+
28+
My favorite color is {myColor}
29+
Your favorite color is {yourColor}
30+
Do we like the same color? {if myColor.Equals yourColor then "Yes" else "No"}
31+
32+
The value of my color ({myColor}) is {Enum.Format(typeof<Colors>, myColor, "d")}
33+
The value of my pet (a {myPet}) is {Enum.Format(typeof<Mammals>, myPet, "d")}
34+
Even though they have the same value, are they equal? {if myColor.Equals myPet then "Yes" else "No"}"""
35+
36+
// The example displays the following output:
37+
// My favorite animal is a Cat
38+
// Your favorite animal is a Dog
39+
// Do we like the same animal? No
40+
//
41+
// My favorite color is Red
42+
// Your favorite color is Red
43+
// Do we like the same color? Yes
44+
//
45+
// The value of my color (Red) is 0
46+
// The value of my pet (a Cat) is 0
47+
// Even though they have the same value, are they equal? No
48+
//</snippet1>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module enumequals1
2+
3+
// <Snippet1>
4+
type SledDog =
5+
| Unknown = 0
6+
| AlaskanMalamute = 1
7+
| Malamute = 1
8+
| Husky = 2
9+
| SiberianHusky = 2
10+
11+
type WorkDog =
12+
| Unknown = 0
13+
| Newfoundland = 1
14+
| GreatPyrennes = 2
15+
16+
let dog1 = SledDog.Malamute
17+
let dog2 = SledDog.AlaskanMalamute
18+
let dog3 = WorkDog.Newfoundland
19+
20+
printfn $"{dog1:F} ({dog1:D}) = {dog2:F} ({dog2:D}): {dog1.Equals dog2}"
21+
printfn $"{dog1:F} ({dog1:D}) = {dog3:F} ({dog3:D}): {dog1.Equals dog3}"
22+
// The example displays the following output:
23+
// Malamute (1) = Malamute (1): True
24+
// Malamute (1) = Newfoundland (1): False
25+
// </Snippet1>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="EnumEquals.fs" />
9+
<Compile Include="enumequals1.fs" />
10+
</ItemGroup>
11+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// <Snippet1>
2+
open System
3+
4+
type Colors =
5+
| Red = 0
6+
| Green = 1
7+
| Blue = 2
8+
| Yellow = 3
9+
10+
let myColor = Colors.Blue
11+
12+
printfn $"My favorite color is {myColor}."
13+
printfn $"""The value of my favorite color is {Enum.Format(typeof<Colors>, myColor, "d")}."""
14+
printfn $"""The hex value of my favorite color is {Enum.Format(typeof<Colors>, myColor, "x")}."""
15+
// The example displays the following output:
16+
// My favorite color is Blue.
17+
// The value of my favorite color is 2.
18+
// The hex value of my favorite color is 00000002.
19+
// </Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="EnumFormat.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//<Snippet1>
2+
open System
3+
4+
type Colors =
5+
| Red = 0
6+
| Green = 1
7+
| Blue = 2
8+
| Yellow = 3
9+
10+
type Styles =
11+
| Plaid = 0
12+
| Striped = 1
13+
| Tartan = 2
14+
| Corduroy = 3
15+
16+
printfn $"The 4th value of the Colors Enum is {Enum.GetName(typeof<Colors>, 3)}"
17+
printfn $"The 4th value of the Styles Enum is {Enum.GetName(typeof<Styles>, 3)}"
18+
// The example displays the following output:
19+
// The 4th value of the Colors Enum is Yellow
20+
// The 4th value of the Styles Enum is Corduroy
21+
//</Snippet1>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="EnumGetName.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module EnumGetNames
2+
3+
//<snippet1>
4+
open System
5+
6+
type Colors =
7+
| Red = 0
8+
| Green = 1
9+
| Blue = 2
10+
| Yellow = 3
11+
12+
type Styles =
13+
| Plaid = 0
14+
| Striped = 1
15+
| Tartan = 2
16+
| Corduroy = 3
17+
18+
printfn "The members of the Colors enum are:"
19+
for s in Enum.GetNames typeof<Colors> do
20+
printfn $"{s}"
21+
22+
printfn "\nThe members of the Styles enum are:"
23+
for s in Enum.GetNames typeof<Styles> do
24+
printfn $"{s}"
25+
// The example displays the following output:
26+
// The members of the Colors enum are:
27+
// Red
28+
// Green
29+
// Blue
30+
// Yellow
31+
//
32+
// The members of the Styles enum are:
33+
// Plaid
34+
// Striped
35+
// Tartan
36+
// Corduroy
37+
//</snippet1>

0 commit comments

Comments
 (0)