|
| 1 | +# Options |
| 2 | + |
| 3 | +The `Option` type is used when an actual value might not exist. |
| 4 | +It avoids the [billion-dollar mistake](https://en.wikipedia.org/wiki/Null_pointer#History) of null references |
| 5 | +by making it impossible to forget to check whether a possibly-missing value is present. |
| 6 | + |
| 7 | +## Remarks |
| 8 | + |
| 9 | +The following code illustrates a function which generates an option type. |
| 10 | + |
| 11 | +```csharp |
| 12 | +using RustyOptions; |
| 13 | + |
| 14 | +Option<int> KeepIfPositive(int x) => x > 0 ? Option.Some(x) : Option<int>.None; |
| 15 | +``` |
| 16 | + |
| 17 | +You can take advantage of the `using static` feature of C# for more concise syntax that is closer to |
| 18 | +that of languages with built-in Option types, such as F# or Rust. |
| 19 | + |
| 20 | +```csharp |
| 21 | +using RustyOptions; |
| 22 | +using static RustyOptions.Option; |
| 23 | + |
| 24 | +Option<int> KeepIfPositive(int x) => x > 0 ? Some(x) : None<int>(); |
| 25 | +``` |
| 26 | + |
| 27 | +The value `None` is used when an option does not have the actual value. |
| 28 | + |
| 29 | +## Using Options |
| 30 | + |
| 31 | +Options are commonly used when a search does not return a matching result, as shown in the following code. |
| 32 | + |
| 33 | +```csharp |
| 34 | +using RustyOptions; |
| 35 | +using static RustyOptions.Option; |
| 36 | + |
| 37 | +Option<T> TryFindMatch<T>(IEnumerable<T> list, Func<T, bool> predicate) |
| 38 | +{ |
| 39 | + foreach (var value in list) |
| 40 | + { |
| 41 | + if (predicate(value)) |
| 42 | + { |
| 43 | + return Some(value); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return None<T>(); |
| 48 | +} |
| 49 | + |
| 50 | +// result1 is Some(100) and its type is Option<int> |
| 51 | +var result1 = TryFindMatch(new[] { 200, 100, 50, 25 }, x => x == 100); |
| 52 | + |
| 53 | +// result2 is None and its type is Option<char> |
| 54 | +var result2 = TryFindMatch(new[] { 'a', 'b', 'c', 'd' }, x => x == 'y'); |
| 55 | +``` |
| 56 | + |
| 57 | +In the previous code, the function `TryFindMatch` takes a list of values and a predicate function |
| 58 | +that returns a Boolean value. The function iterates the list, and if an element is found that satisfies |
| 59 | +the predicate, the iteration ends and the matching value is returned wrapped in a `Some` option. If |
| 60 | +the function reaches the end of the list without finding a match, `None` is returned. |
| 61 | + |
| 62 | +RustyOptions provides extension methods for most collection types. For more information, see [Collections](collections.md). |
| 63 | + |
| 64 | +Options can also be useful when a value might not exist, for example if it is possible that an exception will |
| 65 | +be thrown when you try to construct a value. The following code sample illustrates this. |
| 66 | + |
| 67 | +```csharp |
| 68 | +using System.IO; |
| 69 | +using RustyOptions; |
| 70 | +using static RustyOptions.Option; |
| 71 | + |
| 72 | +Option<FileStream> OpenFile(string filename) |
| 73 | +{ |
| 74 | + try |
| 75 | + { |
| 76 | + var file = File.Open(filename, FileMode.Create); |
| 77 | + return Some(file); |
| 78 | + } |
| 79 | + catch (Exception ex) |
| 80 | + { |
| 81 | + Console.Error.WriteLine("An exception occurred opening file '{0}': {1}", |
| 82 | + filename, ex.Message); |
| 83 | + return None<FileStream>(); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +The `OpenFile` function in the previous example returns a `FileStream` option if the file opens successfully, |
| 89 | +and `None` if an exception occurs. Depending on the situation, it may not be an appropriate design choice to |
| 90 | +catch an exception rather than allowing it to propagate. |
| 91 | + |
| 92 | +## Null Values |
| 93 | + |
| 94 | +Note that unlike the F# Option type, RustyOptions does not allow `Some(null)` - any attempt to create an option |
| 95 | +with a null value will result in `None`. |
| 96 | + |
| 97 | +## Option Properties and Methods |
| 98 | + |
| 99 | +The option type supports the [following properties and methods](../api/RustyOptions.Option-1.yml). |
| 100 | + |
| 101 | +## Converting to Other Types |
| 102 | + |
| 103 | + - Options can be converted to [Results](result.md) using the |
| 104 | + [OkOr](../api/RustyOptions.OptionResultExtensions.yml#RustyOptions_OptionResultExtensions_OkOr__2_RustyOptions_Option___0____1_) |
| 105 | + or [OkOrElse](../api/RustyOptions.OptionResultExtensions.yml#RustyOptions_OptionResultExtensions_OkOrElse__2_RustyOptions_Option___0__Func___1__) extension methods. |
| 106 | + |
| 107 | + - Options can be converted to `IEnumerable<T>` using the [AsEnumerable](../api/RustyOptions.Option-1.yml#RustyOptions_Option_1_AsEnumerable) method. |
| 108 | + |
| 109 | + - Options can be converted to `ReadOnlySpan<T>` using the [AsSpan](../api/RustyOptions.Option-1.yml#RustyOptions_Option_1_AsSpan) method. |
0 commit comments