Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathOptional.cs
More file actions
Latest commit
46 lines (43 loc) · 2.3 KB
/
Copy pathOptional.cs
File metadata and controls
46 lines (43 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
namespacePlotly.NET.CSharp
{
/// <summary>
/// Helper type for handling the special way the Plotly.NET core API uses generics.
/// In short, the problem arises because many optional parameters of Plotly.NET's core API are generics
/// with a type constraint for `IConvertible`. This means that these parameters can be both value and reference types
/// (e.g. `double` and `System.DateTime` both implement IConvertible).
/// If we now have a optional parameter of type `T? where T: IConvertible` the compiler will not allow this
/// without further type constrainst to either reference or value type.
/// This is a problem because we want to 1. allow both, and 2. have a reliable way of determining if the value was not set
/// because the F# API expects to be passed `Option.None` in that case.
/// There exist other workarounds like checking if the value is default or null, but that changes valid default values actually set to null as well.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Value">The value to mark as optional</param>
/// <param name="IsSome">Whether or not the wrapped value is valid. This is used downstream to determine whether to wrap this value into `Option.Some` (if true) or `Option.None` (if false)</param>
publicreadonlyrecordstructOptional<T>(TValue,boolIsSome)
{
/// <summary>
///
/// </summary>
/// <param name="Value"></param>
publicstaticimplicitoperatorOptional<T>(TValue)=>new(Value,true);
}
/// <summary>
/// Extension methods for the `Optional` class
/// </summary>
publicstaticclassOptionalExtensions
{
/// <summary>
/// Converts the `Optional` value to `Some(value)` if the value is valid, or `None` if it is not.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="opt">The `Optional` value to convert to a F# Option</param>
/// <returns>opt converted to `Option`</returns>
staticinternalMicrosoft.FSharp.Core.FSharpOption<T>ToOption<T>(thisOptional<T>opt)=>opt.IsSome?new(opt.Value):Microsoft.FSharp.Core.FSharpOption<T>.None;
}
}