Skip to content

Commit 7c36d40

Browse files
authored
Nullable F# snippets (#7795)
1 parent 147150a commit 7c36d40

File tree

16 files changed

+364
-0
lines changed

16 files changed

+364
-0
lines changed
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="gut.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//<snippet1>
2+
// This code example demonstrates the
3+
// Nullable.GetUnderlyingType() method.
4+
open System
5+
6+
// Declare a type named Example.
7+
// The MyMethod member of Example returns a Nullable of Int32.
8+
9+
type Example() =
10+
member _.MyMethod() =
11+
Nullable 0
12+
13+
(*
14+
Use reflection to obtain a Type object for the Example type.
15+
Use the Type object to obtain a MethodInfo object for the MyMethod method.
16+
Use the MethodInfo object to obtain the type of the return value of
17+
MyMethod, which is Nullable of Int32.
18+
Use the GetUnderlyingType method to obtain the type argument of the
19+
return value type, which is Int32.
20+
*)
21+
let t = typeof<Example>
22+
let mi = t.GetMethod "MyMethod"
23+
let retval = mi.ReturnType
24+
printfn $"Return value type ... {retval}"
25+
let answer = Nullable.GetUnderlyingType retval
26+
printfn $"Underlying type ..... {answer}"
27+
28+
// This code example produces the following results:
29+
// Return value type ... System.Nullable`1[System.Int32]
30+
// Underlying type ..... System.Int32
31+
//</snippet1>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//<snippet1>
2+
// This code example demonstrates the Nullable<T>.Equals
3+
// methods.
4+
open System
5+
6+
let nullInt1 = Nullable 100
7+
let nullInt2 = Nullable 200
8+
9+
// Determine if two nullable of System.Int32 values are equal.
10+
// The nullable objects have different values.
11+
printf "1) nullInt1 and nullInt2 "
12+
if nullInt1.Equals nullInt1 then
13+
printf "are"
14+
else
15+
printf "are not"
16+
printfn " equal."
17+
18+
// Determine if a nullable of System.Int32 and an object
19+
// are equal. The object contains the boxed value of the
20+
// nullable object.
21+
22+
let myObj = box nullInt1
23+
printf "2) nullInt1 and myObj "
24+
if nullInt1.Equals myObj then
25+
printf "are"
26+
else
27+
printf "are not"
28+
printfn " equal."
29+
30+
// This code example produces the following results:
31+
// 1) nullInt1 and nullInt2 are not equal.
32+
// 2) nullInt1 and myObj are equal.
33+
//</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="eq.fs" />
9+
</ItemGroup>
10+
</Project>
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="gvod.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//<snippet1>
2+
// This code example demonstrates the
3+
// Nullable<T>.GetValueOrDefault methods.
4+
open System
5+
6+
// Display the values of two nullable of System.Single structures.
7+
// The printfn string interpolation automatically calls the ToString methods of
8+
// each input argument to display its values. If no value is defined for a
9+
// nullable type, the ToString method for that argument returns the empty
10+
// string ("").
11+
let display title dspMySingle dspYourSingle =
12+
printfn $"{title}) mySingle = [{dspMySingle}], yourSingle = [{dspYourSingle}]"
13+
14+
let mySingle = Nullable 12.34f
15+
let yourSingle = Nullable -1f
16+
17+
[<EntryPoint>]
18+
let main _ =
19+
printfn "*** Display a value or the default value ***\n"
20+
// Display the values of mySingle and yourSingle.
21+
22+
display "A1" mySingle yourSingle
23+
24+
// Shadow the value of mySingle to yourSingle, then display the values
25+
// of mySingle and yourSingle. The yourSingle variable is assigned the
26+
// value 12.34 because mySingle has a value.
27+
28+
let yourSingle = mySingle.GetValueOrDefault()
29+
display "A2" mySingle yourSingle
30+
31+
// Shadow null (Nothing in Visual Basic) to mySingle, which means no value is
32+
// defined for mySingle. Then assign the value of mySingle to yourSingle and
33+
// display the values of both variables. The default value of all binary zeroes
34+
// is assigned to yourSingle because mySingle has no value.
35+
36+
let mySingle = Nullable()
37+
let yourSingle = mySingle.GetValueOrDefault()
38+
display "A3" mySingle yourSingle
39+
40+
// Shadow the original values of mySingle and yourSingle.
41+
let mySingle = Nullable 12.34f
42+
let yourSingle = Nullable -1.0f
43+
44+
printf "\n*** Display a value or the "
45+
printfn "specified default value ***\n"
46+
47+
// Display the values of mySingle and yourSingle.
48+
display "B1" mySingle yourSingle
49+
50+
// Shadow the value of mySingle to yourSingle, then display the values
51+
// of mySingle and yourSingle. The yourSingle variable is assigned the
52+
// value 12.34 because mySingle has a value.
53+
54+
let yourSingle = mySingle.GetValueOrDefault -222.22f
55+
display "B2" mySingle yourSingle
56+
57+
// Shadow null (Nothing in Visual Basic) to mySingle, which means no value is
58+
// defined for mySingle. Then shadow the value of mySingle to yourSingle and
59+
// display the values of both variables. The specified default value of -333.33
60+
// is assigned to yourSingle because mySingle has no value.
61+
62+
let mySingle = Nullable()
63+
let yourSingle = mySingle.GetValueOrDefault -333.33f
64+
display "B3" mySingle yourSingle
65+
0
66+
67+
// This code example produces the following results:
68+
// A1) mySingle = [12.34], yourSingle = [-1]
69+
// A2) mySingle = [12.34], yourSingle = [12.34]
70+
// A3) mySingle = [], yourSingle = [0]
71+
//
72+
// *** Display a value or the specified default value ***
73+
//
74+
// B1) mySingle = [12.34], yourSingle = [-1]
75+
// B2) mySingle = [12.34], yourSingle = [12.34]
76+
// B3) mySingle = [], yourSingle = [-333.33]
77+
//</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="hasvalue2.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// <Snippet1>
2+
open System
3+
4+
let n1 = Nullable 10
5+
let n2 = Nullable()
6+
let mutable n3 = Nullable 20
7+
n3 <- Nullable()
8+
let items = [| n1; n2; n3 |]
9+
10+
for item in items do
11+
printfn $"Has a value: {item.HasValue}"
12+
if item.HasValue then
13+
printfn $"Type: {item.GetType().Name}"
14+
printfn $"Value: {item.Value}"
15+
else
16+
printfn $"Null: {item = Nullable()}"
17+
printfn $"Default Value: {item.GetValueOrDefault()}"
18+
printfn ""
19+
// The example displays the following output:
20+
// Has a value: True
21+
// Type: Int32
22+
// Value: 10
23+
//
24+
// Has a value: False
25+
// Null: True
26+
// Default Value: 0
27+
//
28+
// Has a value: False
29+
// Null: True
30+
// Default Value: 0
31+
// </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="tarow.fs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//<snippet1>
2+
open System
3+
4+
// Define the "titleAuthor" table of the Microsoft "pubs" database.
5+
6+
type titleAuthor =
7+
struct
8+
// Author ID format ###-##-####
9+
val mutable au_id: string
10+
// Title ID format AA####
11+
val mutable title_id: string
12+
// Author ORD is nullable.
13+
val mutable au_ord: Nullable<int16>
14+
// Royalty Percent is nullable.
15+
val mutable royaltyper: Nullable<int>
16+
end
17+
18+
// Display the values of the titleAuthor array elements.
19+
let display dspTitle (dspAllTitleAuthors: #seq<titleAuthor>) =
20+
printfn $"*** {dspTitle} ***"
21+
for dspTA in dspAllTitleAuthors do
22+
printfn $"Author ID ... {dspTA.au_id}"
23+
printfn $"Title ID .... {dspTA.title_id}"
24+
printfn $"Author ORD .. {dspTA.au_ord.GetValueOrDefault -1s}"
25+
printfn $"Royalty %% ... {dspTA.royaltyper.GetValueOrDefault -1}\n"
26+
27+
// Declare and initialize the titleAuthor array.
28+
let ta = Array.zeroCreate<titleAuthor> 3
29+
ta[0].au_id <- "712-32-1176"
30+
ta[0].title_id <- "PS3333"
31+
ta[0].au_ord <- Nullable 1s
32+
ta[0].royaltyper <- Nullable 100
33+
34+
ta[1].au_id <- "213-46-8915"
35+
ta[1].title_id <- "BU1032"
36+
ta[1].au_ord <- Nullable()
37+
ta[1].royaltyper <- Nullable()
38+
39+
ta[2].au_id <- "672-71-3249"
40+
ta[2].title_id <- "TC7777"
41+
ta[2].au_ord <- Nullable()
42+
ta[2].royaltyper <- Nullable 40
43+
44+
// Display the values of the titleAuthor array elements, and
45+
// display a legend.
46+
display "Title Authors Table" ta
47+
printfn "Legend:"
48+
printfn "An Author ORD of -1 means no value is defined."
49+
printfn "A Royalty %% of 0 means no value is defined."
50+
51+
// The example displays the following output:
52+
// *** Title Authors Table ***
53+
// Author ID ... 712-32-1176
54+
// Title ID .... PS3333
55+
// Author ORD .. 1
56+
// Royalty % ... 100
57+
//
58+
// Author ID ... 213-46-8915
59+
// Title ID .... BU1032
60+
// Author ORD .. -1
61+
// Royalty % ... 0
62+
//
63+
// Author ID ... 672-71-3249
64+
// Title ID .... TC7777
65+
// Author ORD .. -1
66+
// Royalty % ... 40
67+
//
68+
// Legend:
69+
// An Author ORD of -1 means no value is defined.
70+
// A Royalty % of 0 means no value is defined.
71+
//</snippet1>

0 commit comments

Comments
 (0)