Skip to content

Commit

Permalink
Documentation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cptjazz committed Nov 15, 2020
1 parent 6a17c6d commit f58c081
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Maybe.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var r2 = await Maybe
.OrElse(99);
```

### Infrastructure methods
### Infrastructure Methods
To fit into the C#/.NET ecosystem we provide the following methods that go beyond pure monadic implementations.
The methods are part of the interface `IMaybe<T>` and `IMaybe` and are implemented _explicitly_ i. e. you must cast to the interface to invoke the methods.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var result = await Try
.Do(() => "I will succeed.")
.ThenAsync(async x => x + " Oh will you?")
.Then(_ => throw new InvalidOperationException())
.Catch<InvalidOperationException>(() =>
.Catch<InvalidOperationException>(ex =>
{
Log.WriteError("Operation did not succeed");
return "failed";
Expand Down
30 changes: 22 additions & 8 deletions Try.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Try<int> tryResult = Try
.Do(_SomeMethod)
.Then(_SomeOtherMethodThatThrows)
.Then(_AndAnotherMethod) // assuming _AndAnotherMethod() returns an int
.Catch<InvalidOperationException>(() =>
.Catch<InvalidOperationException>(ex =>
{
Log.WriteError("Operation did not succeed");
return -99;
Expand All @@ -28,23 +28,20 @@ The constructed `tryResult` will not be executed until `Execute()` is invoked on
int result = tryResult.Execute();
```

The result of the `Execute()` operation is a ``Maybe`1`` because generation of a value is not
guaranteed when an exception occurs.

If no apropriate catch-handler is found, execution of the `Try` throws:

```C#
var result = Try
.Do<int>(() => throw new ArithmeticException())
.Catch<InvalidOperationException>(() =>
.Catch<InvalidOperationException>(ex =>
{
Log.WriteError("Operation did not succeed");
return -99;
})
.Execute(); // throws an ArithmeticException
```

Execution of an empty ``Try`1`` leads to `default(T)`:
Execution of an empty ``Try<T>`` or ``TryAsync<T>`` leads to `default(T)`:

```C#
var result = default(Try<string>).Execute();
Expand All @@ -60,7 +57,7 @@ var result = await Try
.DoAsync(_SomeAsyncMethod)
.ThenAsync(_SomeOtherAsyncMethodThatThrows)
.ThenAsync(_AndAnotherAsyncMethod)
.CatchAsync<InvalidOperationException>(async () =>
.CatchAsync<InvalidOperationException>(async ex =>
{
Log.WriteError("Operation did not succeed");
return -99;
Expand All @@ -76,9 +73,26 @@ var result = await Try
.DoAsync(_SomeAsyncMethod)
.ThenAsync(_SomeOtherAsyncMethodThatThrows)
.ThenAsync(_AndAnotherAsyncMethod)
.CatchAsync<InvalidOperationException>(async () =>
.CatchAsync<InvalidOperationException>(async ex =>
{
Log.WriteError("Operation did not succeed");
return -99;
});
```

### Infrastructure Methods

All ``.Catch<T>(..)`` and ``.CatchAsync<T>(..)`` variants also provide overloads that allow for specifying
the exception type as `Type` object.

```C#
var result = await Try
.DoAsync(_SomeAsyncMethod)
.CatchAsync(
typeof(InvalidOperationException),
async ex =>
{
Log.WriteError("Operation did not succeed");
return -99;
});
```

0 comments on commit f58c081

Please sign in to comment.