DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.13.1" />

.NET API 582,040 bytes

 Optional

public static class Optional
Various extension and factory methods for constructing optional value.
using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading.Tasks; namespace DotNext { public static class Optional { [AsyncStateMachine(typeof(<OrNull>d__0<>))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] public static Task<T?> OrNull<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 0 })] this Task<Optional<T>> task) where T : struct { <OrNull>d__0<T> stateMachine = default(<OrNull>d__0<T>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<T?>.Create(); stateMachine.task = task; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(2)] [AsyncStateMachine(typeof(<Or>d__1<>))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public static Task<T> Or<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] this Task<Optional<T>> task, T defaultValue) { <Or>d__1<T> stateMachine = default(<Or>d__1<T>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<T>.Create(); stateMachine.task = task; stateMachine.defaultValue = defaultValue; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(2)] [AsyncStateMachine(typeof(<Convert>d__2<, >))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] public static Task<Optional<TOutput>> Convert<TInput, TOutput>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] this Task<Optional<TInput>> task, [System.Runtime.CompilerServices.Nullable(1)] Converter<TInput, TOutput> converter) { <Convert>d__2<TInput, TOutput> stateMachine = default(<Convert>d__2<TInput, TOutput>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<Optional<TOutput>>.Create(); stateMachine.task = task; stateMachine.converter = converter; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(1)] [AsyncStateMachine(typeof(<OrThrow>d__3<, >))] public static Task<T> OrThrow<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TException>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] this Task<Optional<T>> task) where TException : Exception, new { <OrThrow>d__3<T, TException> stateMachine = default(<OrThrow>d__3<T, TException>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<T>.Create(); stateMachine.task = task; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(1)] [AsyncStateMachine(typeof(<OrThrow>d__4<, >))] public static Task<T> OrThrow<[System.Runtime.CompilerServices.Nullable(2)] T, [System.Runtime.CompilerServices.Nullable(0)] TException>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] this Task<Optional<T>> task, Func<TException> exceptionFactory) where TException : Exception { <OrThrow>d__4<T, TException> stateMachine = default(<OrThrow>d__4<T, TException>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<T>.Create(); stateMachine.task = task; stateMachine.exceptionFactory = exceptionFactory; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(1)] [AsyncStateMachine(typeof(<OrInvoke>d__5<>))] public static Task<T> OrInvoke<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] this Task<Optional<T>> task, Func<T> defaultFunc) { <OrInvoke>d__5<T> stateMachine = default(<OrInvoke>d__5<T>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<T>.Create(); stateMachine.task = task; stateMachine.defaultFunc = defaultFunc; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(2)] [AsyncStateMachine(typeof(<OrDefault>d__6<>))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 2 })] public static Task<T> OrDefault<T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] this Task<Optional<T>> task) { <OrDefault>d__6<T> stateMachine = default(<OrDefault>d__6<T>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<T>.Create(); stateMachine.task = task; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(1)] [AsyncStateMachine(typeof(<If>d__7<>))] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] public static Task<Optional<T>> If<[System.Runtime.CompilerServices.Nullable(2)] T>([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0, 1 })] this Task<Optional<T>> task, Predicate<T> condition) { <If>d__7<T> stateMachine = default(<If>d__7<T>); stateMachine.<>t__builder = AsyncTaskMethodBuilder<Optional<T>>.Create(); stateMachine.task = task; stateMachine.condition = condition; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(1)] public static bool IsOptional(this Type optionalType) { if (optionalType.IsConstructedGenericType) return optionalType.GetGenericTypeDefinition() == typeof(Optional<>); return false; } [System.Runtime.CompilerServices.NullableContext(1)] [return: System.Runtime.CompilerServices.Nullable(2)] public static Type GetUnderlyingType(Type optionalType) { if (!optionalType.IsOptional()) return null; return optionalType.GetGenericArguments()[0]; } public static Optional<T> ToOptional<T>([In] [IsReadOnly] ref T? value) where T : struct { if (!value.HasValue) return None<T>(); return Some(value.GetValueOrDefault()); } public static T? OrNull<T>([In] [IsReadOnly] ref Optional<T> value) where T : struct { if (!value.HasValue) return null; return value.OrDefault(); } [System.Runtime.CompilerServices.NullableContext(2)] [return: IsReadOnly] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static ref Optional<T> Coalesce<T>([In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ref Optional<T> first, [In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] ref Optional<T> second) { if (!first.HasValue) return ref second; return ref first; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> None<T>() { return Optional<T>.None; } [System.Runtime.CompilerServices.NullableContext(1)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> Some<[System.Runtime.CompilerServices.Nullable(2)] T>([DisallowNull] T value) { return new Optional<T>(value); } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> Null<T>() where T : class { return new Optional<T>((T)null); } [return: IsReadOnly] private static ref T GetReference<T, TException>([In] [IsReadOnly] ref Optional<T> optional, TException exceptionFactory) where T : struct where TException : struct, ISupplier<Exception> { optional.Validate(exceptionFactory); return ref Optional<T>.GetReference(ref optional); } [return: IsReadOnly] public static ref T GetReference<T, TException>([In] [IsReadOnly] ref Optional<T> optional) where T : struct where TException : Exception, new { return ref GetReference(ref optional, default(Activator<TException>)); } [return: IsReadOnly] public static ref T GetReference<T>([In] [IsReadOnly] ref Optional<T> optional, [System.Runtime.CompilerServices.Nullable(1)] Func<Exception> exceptionFactory) where T : struct { return ref GetReference(ref optional, (DelegatingSupplier<Exception>)exceptionFactory); } [CLSCompliant(false)] [return: IsReadOnly] public static ref T GetReference<T>([In] [IsReadOnly] ref Optional<T> optional, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] IntPtr exceptionFactory) where T : struct { return ref GetReference(ref optional, (Supplier<Exception>)(long)exceptionFactory); } [return: IsReadOnly] public static ref T GetReference<T>([In] [IsReadOnly] ref Optional<T> optional) where T : struct { optional.Validate(); return ref Optional<T>.GetReference(ref optional); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> Create<[System.Runtime.CompilerServices.Nullable(2)] T, TMonad>(TMonad value) where TMonad : struct, IOptionMonad<T> { if (!((IOptionMonad<T>)value).TryGet(out T value2)) return None<T>(); return new Optional<T>(value2); } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<T> Flatten<T>([In] [IsReadOnly] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 0, 1 })] ref Optional<Optional<T>> optional) { return new Optional<T>(ref optional); } } }