Skip to content

feature/monad-async #56

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

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion OnixLabs.Core/OnixLabs.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<NeutralLanguage>en</NeutralLanguage>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.2.0</PackageVersion>
<PackageVersion>8.3.0</PackageVersion>
<PackageLicenseUrl></PackageLicenseUrl>
</PropertyGroup>
<PropertyGroup>
Expand Down
44 changes: 44 additions & 0 deletions OnixLabs.Core/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

using System;
using System.Threading.Tasks;

namespace OnixLabs.Core;

Expand Down Expand Up @@ -60,6 +61,28 @@ public static Result Of(Action action)
}
}

/// <summary>
/// Creates a new instance of the <see cref="Result"/> class, where the underlying value is the result of a successful invocation
/// of the specified function; otherwise, the underlying value is the exception thrown by a failed invocation of the specified function.
/// </summary>
/// <param name="func">The function to invoke to obtain a successful or failed result.</param>
/// <returns>
/// Returns a new instance of the <see cref="Result"/> class, where the underlying value is the result of a successful invocation
/// of the specified function; otherwise, the underlying value is the exception thrown by a failed invocation of the specified function.
/// </returns>
public static async Task<Result> OfAsync(Func<Task> func)
{
try
{
await func();
return Success();
}
catch (Exception exception)
{
return exception;
}
}

/// <summary>
/// Creates a new instance of the <see cref="Result"/> class, where the underlying value represents a successful result.
/// </summary>
Expand Down Expand Up @@ -231,6 +254,27 @@ public static Result<T> Of(Func<T> func)
}
}

/// <summary>
/// Creates a new instance of the <see cref="Result{T}"/> class, where the underlying value is the result of a successful invocation
/// of the specified function; otherwise, the underlying value is the exception thrown by a failed invocation of the specified function.
/// </summary>
/// <param name="func">The function to invoke to obtain a successful or failed result.</param>
/// <returns>
/// Returns a new instance of the <see cref="Result{T}"/> class, where the underlying value is the result of a successful invocation
/// of the specified function; otherwise, the underlying value is the exception thrown by a failed invocation of the specified function.
/// </returns>
public static async Task<Result<T>> OfAsync(Func<Task<T>> func)
{
try
{
return await func();
}
catch (Exception exception)
{
return exception;
}
}

/// <summary>
/// Creates a new instance of the <see cref="Result{T}"/> class, where the underlying value represents a successful result.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion OnixLabs.Numerics/OnixLabs.Numerics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.2.0</PackageVersion>
<PackageVersion>8.3.0</PackageVersion>
<LangVersion>12</LangVersion>
<PackageLicenseUrl></PackageLicenseUrl>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Copyright © ONIXLabs 2020</Copyright>
<RepositoryUrl>https://github.com/onix-labs/onixlabs-dotnet</RepositoryUrl>
<PackageVersion>8.2.0</PackageVersion>
<PackageVersion>8.3.0</PackageVersion>
<LangVersion>12</LangVersion>
<PackageLicenseUrl></PackageLicenseUrl>
</PropertyGroup>
Expand Down
Loading