Skip to content

Commit d0011cd

Browse files
committed
Optional and Result Extensions for quick creation
1 parent 7d3b2ae commit d0011cd

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/DotNext/Optional.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public static async Task<Optional<TOutput>> Convert<TInput, TOutput>(this Task<O
7272
public static async Task<Optional<TOutput>> Convert<TInput, TOutput>(this Task<Optional<TInput>> task, Converter<TInput, Task<Optional<TOutput>>> converter)
7373
=> await (await task.ConfigureAwait(false)).Convert(converter).ConfigureAwait(false);
7474

75+
/// <summary>
76+
/// Creates a new instance of <see cref="Optional{T}"/> from the specified value.
77+
/// </summary>
78+
/// <typeparam name="T">The type of the value.</typeparam>
79+
/// <param name="value">The value to be placed to the container.</param>
80+
/// <returns>The value encapsulated by <see cref="Result{T}"/>.</returns>
81+
public static Optional<T> FromValue<T>(T value) => new(value);
82+
7583
/// <summary>
7684
/// Creates <see cref="Result{T}"/> from <see cref="Optional{T}"/> instance.
7785
/// </summary>

src/DotNext/Result.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public static class Result
6868
/// <returns>The value encapsulated by <see cref="Result{T}"/>.</returns>
6969
public static Result<T> FromValue<T>(T value) => new(value);
7070

71+
/// <summary>
72+
/// Creates a new instance of <see cref="Result{T}"/> from the specified value.
73+
/// </summary>
74+
/// <typeparam name="T">The type of the value.</typeparam>
75+
/// <typeparam name="TError">The type of the error code. Default value must represent the successful result.</typeparam>
76+
/// <param name="value">The value to be placed to the container.</param>
77+
/// <returns>The value encapsulated by <see cref="Result{T, TError}"/>.</returns>
78+
public static Result<T, TError> FromValue<T, TError>(T value)
79+
where TError: struct, Enum
80+
=> new(value);
81+
7182
/// <summary>
7283
/// Creates a new instance of <see cref="Result{T}"/> from the specified exception.
7384
/// </summary>
@@ -76,6 +87,17 @@ public static class Result
7687
/// <returns>The exception encapsulated by <see cref="Result{T}"/>.</returns>
7788
public static Result<T> FromException<T>(Exception e) => new(e);
7889

90+
/// <summary>
91+
/// Creates a new instance of <see cref="Result{T, TError}"/> from the specified exception.
92+
/// </summary>
93+
/// <typeparam name="T">The type of the value.</typeparam>
94+
/// <typeparam name="TError">The type of the error code. Default value must represent the successful result.</typeparam>
95+
/// <param name="e">The error to be placed to the container.</param>
96+
/// <returns>The exception encapsulated by <see cref="Result{T}"/>.</returns>
97+
public static Result<T, TError> FromError<T, TError>(TError e)
98+
where TError: struct, Enum
99+
=> new(e);
100+
79101
/// <summary>
80102
/// If successful result is present, apply the provided mapping function hiding any exception
81103
/// caused by the converter.

0 commit comments

Comments
 (0)