-
Notifications
You must be signed in to change notification settings - Fork 141
Added convenience methods for easier Monad creation and Monad chaining #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
julianthurner
wants to merge
4
commits into
dotnet:develop
Choose a base branch
from
julianthurner:master
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
|
||
namespace DotNext; | ||
|
||
using System; | ||
using DotNext.Threading.Tasks; | ||
using Runtime.CompilerServices; | ||
using Intrinsics = Runtime.Intrinsics; | ||
|
||
|
@@ -202,6 +204,53 @@ private Result<TResult> Convert<TResult, TConverter>(TConverter converter) | |
return result; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private Result<TResult> ConvertResult<TResult, TConverter>(TConverter converter) | ||
where TConverter : struct, ISupplier<T, Result<TResult>> | ||
{ | ||
Result<TResult> result; | ||
if (exception is null) | ||
{ | ||
try | ||
{ | ||
result = converter.Invoke(value); | ||
} | ||
catch (Exception e) | ||
{ | ||
result = new(e); | ||
} | ||
} | ||
else | ||
{ | ||
result = new(exception); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private AwaitableResult<TResult> ConvertTask<TResult>(Func<T, CancellationToken, Task<TResult>> converter, CancellationToken token = default) | ||
{ | ||
AwaitableResult<TResult> result; | ||
if (exception is null) | ||
{ | ||
try | ||
{ | ||
result = converter.Invoke(value, token).SuspendException(); | ||
} | ||
catch (Exception e) | ||
{ | ||
result = new(Task.FromException<TResult>(e)); | ||
} | ||
} | ||
else | ||
{ | ||
result = new(Task.FromException<TResult>(exception.SourceException)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// If the successful result is present, apply the provided mapping function hiding any exception | ||
/// caused by the converter. | ||
|
@@ -212,6 +261,27 @@ private Result<TResult> Convert<TResult, TConverter>(TConverter converter) | |
public Result<TResult> Convert<TResult>(Converter<T, TResult> converter) | ||
=> Convert<TResult, DelegatingConverter<T, TResult>>(converter); | ||
|
||
/// <summary> | ||
/// If successful result is present, apply the provided mapping function. If not, | ||
/// forward the exception. | ||
/// </summary> | ||
/// <param name="converter">A mapping function to be applied to the value, if present.</param> | ||
/// <typeparam name="TResult">The type of the result of the mapping function.</typeparam> | ||
/// <returns>The conversion result.</returns> | ||
public Result<TResult> Convert<TResult>(Converter<T, Result<TResult>> converter) | ||
=> ConvertResult<TResult, DelegatingConverter<T, Result<TResult>>>(converter); | ||
|
||
/// <summary> | ||
/// If successful result is present, apply the provided mapping function. If not, | ||
/// forward the exception. | ||
/// </summary> | ||
/// <param name="converter">A mapping function to be applied to the value, if present.</param> | ||
/// <typeparam name="TResult">The type of the result of the mapping function.</typeparam> | ||
/// <param name="token">The token that can be used to cancel the operation.</param> | ||
/// <returns>The conversion result.</returns> | ||
public AwaitableResult<TResult> Convert<TResult>(Func<T, CancellationToken, Task<TResult>> converter, CancellationToken token = default) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
=> ConvertTask(converter, token); | ||
|
||
/// <summary> | ||
/// If the successful result is present, apply the provided mapping function hiding any exception | ||
/// caused by the converter. | ||
|
@@ -223,6 +293,17 @@ public Result<TResult> Convert<TResult>(Converter<T, TResult> converter) | |
public unsafe Result<TResult> Convert<TResult>(delegate*<T, TResult> converter) | ||
=> Convert<TResult, Supplier<T, TResult>>(converter); | ||
|
||
/// <summary> | ||
/// If successful result is present, apply the provided mapping function. If not, | ||
/// forward the exception. | ||
/// </summary> | ||
/// <param name="converter">A mapping function to be applied to the value, if present.</param> | ||
/// <typeparam name="TResult">The type of the result of the mapping function.</typeparam> | ||
/// <returns>The conversion result.</returns> | ||
[CLSCompliant(false)] | ||
public unsafe Result<TResult> Convert<TResult>(delegate*<T, Result<TResult>> converter) | ||
=> ConvertResult<TResult, Supplier<T, Result<TResult>>>(converter); | ||
|
||
/// <summary> | ||
/// Attempts to extract value from the container if it is present. | ||
/// </summary> | ||
|
@@ -323,6 +404,13 @@ public ValueTask<T> AsTask() | |
{ } error => ValueTask.FromException<T>(error), | ||
}; | ||
|
||
/// <summary> | ||
/// Converts this result to <see cref="AwaitableResult{TResult}"/>. | ||
/// </summary> | ||
/// <returns>The awaitable Result representing the result.</returns> | ||
public AwaitableResult<T> ToAwaitable() | ||
=> IsSuccessful ? new(Task.FromResult(value)) : new(Task.FromException<T>(Error)); | ||
|
||
/// <summary> | ||
/// Converts the result to <see cref="Task{TResult}"/>. | ||
/// </summary> | ||
|
@@ -534,6 +622,11 @@ private Result<TResult, TError> Convert<TResult, TConverter>(TConverter converte | |
where TConverter : struct, ISupplier<T, TResult> | ||
=> IsSuccessful ? new(converter.Invoke(value)) : new(Error); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private Result<TResult, TError> ConvertResult<TResult, TConverter>(TConverter converter) | ||
where TConverter : struct, ISupplier<T, Result<TResult, TError>> | ||
=> IsSuccessful ? converter.Invoke(value) : new(Error); | ||
|
||
/// <summary> | ||
/// If the successful result is present, apply the provided mapping function hiding any exception | ||
/// caused by the converter. | ||
|
@@ -544,6 +637,16 @@ private Result<TResult, TError> Convert<TResult, TConverter>(TConverter converte | |
public Result<TResult, TError> Convert<TResult>(Converter<T, TResult> converter) | ||
=> Convert<TResult, DelegatingConverter<T, TResult>>(converter); | ||
|
||
/// <summary> | ||
/// If successful result is present, apply the provided mapping function. If not, | ||
/// forward the error. | ||
/// </summary> | ||
/// <param name="converter">A mapping function to be applied to the value, if present.</param> | ||
/// <typeparam name="TResult">The type of the result of the mapping function.</typeparam> | ||
/// <returns>The conversion result.</returns> | ||
public Result<TResult, TError> Convert<TResult>(Converter<T, Result<TResult, TError>> converter) | ||
=> ConvertResult<TResult, DelegatingConverter<T, Result<TResult, TError>>>(converter); | ||
|
||
/// <summary> | ||
/// If the successful result is present, apply the provided mapping function hiding any exception | ||
/// caused by the converter. | ||
|
@@ -555,6 +658,17 @@ public Result<TResult, TError> Convert<TResult>(Converter<T, TResult> converter) | |
public unsafe Result<TResult, TError> Convert<TResult>(delegate*<T, TResult> converter) | ||
=> Convert<TResult, Supplier<T, TResult>>(converter); | ||
|
||
/// <summary> | ||
/// If successful result is present, apply the provided mapping function. If not, | ||
/// forward the error. | ||
/// </summary> | ||
/// <param name="converter">A mapping function to be applied to the value, if present.</param> | ||
/// <typeparam name="TResult">The type of the result of the mapping function.</typeparam> | ||
/// <returns>The conversion result.</returns> | ||
[CLSCompliant(false)] | ||
public unsafe Result<TResult, TError> Convert<TResult>(delegate*<T, Result<TResult, TError>> converter) | ||
=> ConvertResult<TResult, Supplier<T, Result<TResult, TError>>>(converter); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
private T OrInvoke<TSupplier>(TSupplier defaultFunc) | ||
where TSupplier : struct, ISupplier<T> | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is it differ from existing
Optional.Convert
extension method? It allows conversion fromTInput
toOptional<TOutput>
. Moreover, there is alwaysFlatten
method that you can insert in the beginning of your chain to convertTask<Optional<T>>
to plainTask<T>
. Then, you can finalize your chain withSuspendException
that returnsResult<T>
with implicit conversion toOptional<T>
. Could you provide real-world example that demonstrates the real need of this conversion?