Skip to content

Commit

Permalink
Edit Maybe and Result README.md
Browse files Browse the repository at this point in the history
Update Release note.

Fix comments.
  • Loading branch information
KeRNeLith committed Aug 1, 2018
1 parent 4f5d9e6 commit 7124730
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/Here/Here.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ Supported platforms:
<Company>Alexandre Rabérin</Company>
<IsPackable>true</IsPackable>
<PackageId>Here</PackageId>
<PackageReleaseNotes>➟ Release 0.2.0
- For Maybe:
- Little optimizations
- New IfOr and ElseOr extensions.
- Easy conversions between numeric Maybe (ToInt(), ToLong(), ToDouble(), etc.)
- Conversion from numeric Maybe to Maybe&lt;bool&gt;.
- Try Get Value for dictionaries supports null key queries.
- Try Get Value for dictionary&lt;TKey, object&gt; supports value cast to an expected type.</PackageReleaseNotes>
<PackageReleaseNotes>➟ Release 0.3.0
- General:
- Add missing JetBrains annotations.

- For Maybes:
- Add explicit and implicit converters to Result/Result&lt;T&gt;/CustomResult&lt;T, TError&gt;/Result&lt;T, TError&gt;.

- For Results:
- Result can now always embed an Exception (for Warning and Failure).
- Add scopes to safely return a Result of any type.
- Add explicit and implicit converters to Maybe&lt;T&gt;.</PackageReleaseNotes>
<PackageTags>Here Functional C# Maybe Monad Result</PackageTags>
<PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/KeRNeLith/Here</PackageProjectUrl>
Expand Down
20 changes: 20 additions & 0 deletions src/Here/Maybe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,24 @@ Maybe<int> maybeInt = maybeDouble.ToInt(); // 42
// As a consequence you can chain calls like this:
string myString = "51.52";
Maybe<double> maybeDouble = myString.TryParseInt().ToDouble(); // 51
```

### Bridge to Result

It is possible to convert a `Maybe<T>` to a `Result`, `Result<T>`, `CustomResult<TError>` or `Result<T, TError>`.

For the first two conversions, both also support implicit conversion.

```csharp
var maybeInt = Maybe<int>.Some(42);

Result result = maybeInt.ToResult(); // Explicit => Result.OK
Result result = maybeInt; // Implicit => Result.OK

var emptyMaybeInt = Maybe<int>.None;

Result result = emptyMaybeInt.ToResult(); // Explicit => Result.Fail
Result result = emptyMaybeInt.ToResult("Custom failure message"); // Explicit => Result.Fail
Result result = emptyMaybeInt; // Implicit => Result.Fail
```
75 changes: 74 additions & 1 deletion src/Here/Result/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ See below for some examples:
Result simpleResult = Result.Ok();
simpleResult = Result.Warn("Your Warning message.");
simpleResult = Result.Fail("Your ERROR message.");
simpleResult = Result.Fail("Your ERROR message.", new Exception("Embedded exception")); // Example with an embedded exception
// Result with value
Result<int> resultValue = Result.Ok(42);
Expand Down Expand Up @@ -105,4 +106,76 @@ So the only to have a failed result is obviously to use the `Fail` construction,

The necessity to have a message for warnings and errors is motivated by the need to force the developer of a treatment to explain error cases.

Then you have `IResult<T>` and `IResultError<TError>` that respectively provide a `Value` and a custom `Error`.
Then you have `IResult<T>` and `IResultError<TError>` that respectively provide a `Value` and a custom `Error`.

### Safe Scopes

If you want to run code that should return a Result safely, you can use Result scopes to do this.

There is at least one per Result type. Following is an example with the scope for `Result`.

```csharp
// Example 1
public Result MyFunction()
{
return ResultScope.SafeResult(() =>
{
// ...
// Your code
return Result.Ok();
});
}

// The call will give
var result = MyFunction(); // Result.Ok()
// Example 2
public Result MyFunctionRaiseException()
{
return ResultScope.SafeResult(() =>
{
// ...
// Your code that trigger an exception
return Result.Ok();
});
}

// The call will give
var result = MyFunctionRaiseException(); // Result.Fail()
// The scope catch exception and produce a Result that embed the thrown exception.
// So you can keep focus on your code rather than exception that it can trigger.
```

### Bridge to Maybe

It is possible to convert a `Result`, `Result<T>`, `CustomResult<TError>` or `Result<T, TError>` to a `Maybe<T>`.

Conversions from a `Result` or a `CustomResult<TError>` give a `Maybe<bool>`, the other give a `Maybe<T>`.
Each conversion can be done implicitly too.

```csharp
// Result without value
Result resultOK = Result.Ok();

Maybe<bool> maybeBool = resultOK.ToMaybe(); // Explicit => Maybe.Some(true)
Maybe<bool> maybeBool = resultOK; // Implicit => Maybe.Some(true)
Result resultFail = Result.Fail("Failure");

Maybe<bool> maybeBool = resultFail.ToMaybe(); // Explicit => Maybe.Some(false)
Maybe<bool> maybeBool = resultFail; // Implicit => Maybe.Some(false)
// Result with value
Result<int> resultOK = Result.Ok(12);

Maybe<bool> maybeBool = resultOK.ToMaybe(); // Explicit => Maybe.Some(12)
Maybe<bool> maybeBool = resultOK; // Implicit => Maybe.Some(12)
Result<int> resultFail = Result.Fail<int>("Failure");

Maybe<bool> maybeBool = resultFail.ToMaybe(); // Explicit => Maybe.None
Maybe<bool> maybeBool = resultFail; // Implicit => Maybe.None
```
4 changes: 2 additions & 2 deletions src/Here/Result/ResultScopes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static CustomResult<TError> SafeCustomResult<TError>([NotNull, InstantHan
/// Run the given <paramref name="action"/> in a safe scope that always return a <see cref="Result"/>.
/// </summary>
/// <param name="action">Function to call.</param>
/// <param name="errorFactory">Function to create a custom error objec in case an exception is thrown.</param>
/// <param name="errorFactory">Function to create a custom error object in case an exception is thrown.</param>
/// <returns>A <see cref="CustomResult{TError}"/>.</returns>
[PublicAPI]
public static CustomResult<TError> SafeCustomResult<TError>([NotNull, InstantHandle] Func<CustomResult<TError>> action, [NotNull] Func<TError> errorFactory)
Expand Down Expand Up @@ -135,7 +135,7 @@ public static Result<T, TError> SafeValueCustomResult<T, TError>([NotNull, Insta
/// Run the given <paramref name="action"/> in a safe scope that always return a <see cref="Result"/>.
/// </summary>
/// <param name="action">Function to call.</param>
/// <param name="errorFactory">Function to create a custom error objec in case an exception is thrown.</param>
/// <param name="errorFactory">Function to create a custom error object in case an exception is thrown.</param>
/// <returns>A <see cref="Result{T, TError}"/>.</returns>
[PublicAPI]
public static Result<T, TError> SafeValueCustomResult<T, TError>([NotNull, InstantHandle] Func<Result<T, TError>> action, [NotNull] Func<TError> errorFactory)
Expand Down

0 comments on commit 7124730

Please sign in to comment.