Skip to content

Commit 4089e7d

Browse files
committed
Option NoneValue, NoneValueAsync
1 parent 9b72fdc commit 4089e7d

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/Danom/Option/Option.cs

+31-5
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,36 @@ public static Task<Option<T>> SomeAsync(T value) =>
152152
/// Creates a new <see cref="Option{T}"/> with the value of the awaited Task.
153153
/// </summary>
154154
/// <param name="value"></param>
155+
/// <param name="cancellationToken"></param>
155156
/// <returns></returns>
156-
public static async Task<Option<T>> SomeAsync(Task<T> value) =>
157-
Some(await value);
157+
public static async Task<Option<T>> SomeAsync(
158+
Task<T> value,
159+
CancellationToken? cancellationToken = null)
160+
{
161+
var result = await value.WaitOrCancel(cancellationToken);
162+
return Some(result);
163+
}
164+
165+
/// <summary>
166+
/// An Option of <see cref="Option{T}"/> with no value.
167+
/// </summary>
168+
/// <returns></returns>
169+
public static Option<T> NoneValue =>
170+
new();
158171

159172
/// <summary>
160173
/// Creates a new <see cref="Option{T}"/> with no value.
161174
/// </summary>
162175
/// <returns></returns>
163176
public static Option<T> None() =>
164-
new();
177+
NoneValue;
178+
179+
/// <summary>
180+
/// An Option of <see cref="Option{T}"/> with no value wrapped in a completed Task.
181+
/// </summary>
182+
/// <returns></returns>
183+
public static Task<Option<T>> NoneValueAsync =>
184+
Task.FromResult(None());
165185

166186
/// <summary>
167187
/// Creates a new <see cref="Option{T}"/> with no value wrapped in a completed Task.
@@ -280,7 +300,13 @@ public static Task<Option<T>> SomeAsync<T>(T value) =>
280300
/// Creates a new <see cref="Option{T}"/> with the value of the awaited Task.
281301
/// </summary>
282302
/// <param name="value"></param>
303+
/// <param name="cancellationToken"></param>
283304
/// <returns></returns>
284-
public static async Task<Option<T>> SomeAsync<T>(Task<T> value) =>
285-
Option<T>.Some(await value);
305+
public static async Task<Option<T>> SomeAsync<T>(
306+
Task<T> value,
307+
CancellationToken? cancellationToken = null)
308+
{
309+
var result = await value.WaitOrCancel(cancellationToken);
310+
return Option<T>.Some(result);
311+
}
286312
}

0 commit comments

Comments
 (0)