diff --git a/.gitignore b/.gitignore
index f1e3d20..67cad60 100644
--- a/.gitignore
+++ b/.gitignore
@@ -250,3 +250,5 @@ paket-files/
# JetBrains Rider
.idea/
*.sln.iml
+
+*.orig
diff --git a/Monacs.Core/Result.Extensions.cs b/Monacs.Core/Result.Extensions.cs
index 5ca1f87..f9529de 100644
--- a/Monacs.Core/Result.Extensions.cs
+++ b/Monacs.Core/Result.Extensions.cs
@@ -24,7 +24,7 @@ public static partial class Result
/// Desired type parameter for type.
/// Details of the error.
public static Result Error(ErrorDetails error) => new Result(error);
-
+
/* Converters */
///
@@ -400,8 +400,6 @@ public static Result> Sequence(this IEnumerable> ite
? Error>(items.First(i => i.IsError).Error)
: Ok(items.Select(i => i.Value));
- /* TryCatch */
-
///
/// Tries to execute .
/// If the execution completes without exception, returns Ok with the function result.
@@ -410,19 +408,6 @@ public static Result> Sequence(this IEnumerable> ite
/// Type of the value in the result.
/// Function to execute.
/// Function that generates error details in case of exception.
- public static Result TryCatch(Func func, Func errorHandler)
- {
- try
- {
- var result = func();
- return Ok(result);
- }
- catch (Exception ex)
- {
- return Error(errorHandler(ex));
- }
- }
-
///
/// Tries to execute with the value from the as an input.
/// If the execution completes without exception, returns Ok with the function result.
@@ -434,7 +419,5 @@ public static Result TryCatch(Func func, Func
/// Result to take the value from.
/// Function to execute.
/// Function that generates error details in case of exception.
- public static Result TryCatch(this Result result, Func func, Func errorHandler) =>
- result.Bind(value => Result.TryCatch(() => func(value), e => errorHandler(value, e)));
}
}
diff --git a/Monacs.Core/Result.TryCatch.Extensions.cs b/Monacs.Core/Result.TryCatch.Extensions.cs
new file mode 100644
index 0000000..9511899
--- /dev/null
+++ b/Monacs.Core/Result.TryCatch.Extensions.cs
@@ -0,0 +1,74 @@
+using System;
+
+namespace Monacs.Core
+{
+ public static partial class Result
+ {
+ ///
+ /// Invokes function in try/catch block and returns its result.
+ /// If any is raised during execution, error handler is invoked and error details are returned.
+ ///
+ /// Type of value returned by invoked function.
+ /// The function to be invoked in 'try' block.
+ /// Handler invoked in 'catch' block on any raised exception.
+ /// of invoked function in try block or if any exception occurs.
+ public static Result TryCatch(Func func, Func errorHandler)
+ {
+ try
+ {
+ return Ok(func());
+ }
+ catch (Exception ex)
+ {
+ return Error(errorHandler(ex));
+ }
+ }
+
+ ///
+ /// Invokes function in try/catch block and returns its result.
+ /// If any is raised during execution, error handler is invoked and error details are returned.
+ ///
+ /// Type of an argument accepted by invoked function.
+ /// Type of value returned by invoked function.
+ /// Result of previous operation.
+ /// The function to be invoked in 'try' block.
+ /// Handler invoked in 'catch' block on any raised exception.
+ /// of invoked function in try block or if any exception occurs.
+ public static Result TryCatch(this Result result, Func func, Func errorHandler) =>
+ result.Bind(value => Result.TryCatch(() => func(value), e => errorHandler(value, e)));
+
+ ///
+ /// Invokes function in try/catch block and returns its result.
+ ///
+ /// Type of value returned by invoked function.
+ /// Type of first value in function tuple argument.
+ /// Type of second value in function tuple argument.
+ /// Result of previous operation.
+ /// The function to be invoked in 'try' block.
+ /// Handler invoked in 'catch' block on any raised exception.
+ /// of invoked function in try block or if any exception occurs.
+ public static Result TryCatch2(
+ this Result<(TFst fst, TSnd snd)> result,
+ Func tryFunc,
+ Func errorHandler) =>
+ result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd),
+ errorHandler: err => errorHandler(value.fst, value.snd, err)));
+
+ ///
+ /// Invokes function in try/catch block and returns its result.
+ ///
+ /// Type of value returned by invoked function.
+ /// Type of first value in function tuple argument.
+ /// Type of second value in function tuple argument.
+ /// Result of previous operation.
+ /// The function to be invoked in 'try' block.
+ /// Handler invoked in 'catch' block on any raised exception.
+ /// of invoked function in try block or if any exception occurs.
+ public static Result TryCatch3(
+ this Result<(TFst fst, TSnd snd, TTrd trd)> result,
+ Func tryFunc,
+ Func errorHandler) =>
+ result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd, value.trd),
+ errorHandler: err => errorHandler(value.fst, value.snd, value.trd, err)));
+ }
+}
diff --git a/Monacs.Core/Tuples/Result.Tuple.Extensions.cs b/Monacs.Core/Tuples/Result.Tuple.Extensions.cs
index 5299d6a..dfcbdf3 100644
--- a/Monacs.Core/Tuples/Result.Tuple.Extensions.cs
+++ b/Monacs.Core/Tuples/Result.Tuple.Extensions.cs
@@ -59,41 +59,5 @@ public static TVal Match3(this Result<(TFst fst, TSnd sn
return result;
}
- /* Try/Catch */
-
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- public static Result TryCatch2(
- this Result<(TFst fst, TSnd snd)> result,
- Func tryFunc,
- Func errorHandler) =>
- result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd),
- errorHandler: err => errorHandler(value.fst, value.snd, err)));
-
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- ///
- public static Result TryCatch3(
- this Result<(TFst fst, TSnd snd, TTrd trd)> result,
- Func tryFunc,
- Func errorHandler) =>
- result.Bind(value => TryCatch(func: () => tryFunc(value.fst, value.snd, value.trd),
- errorHandler: err => errorHandler(value.fst, value.snd, value.trd, err)));
}
}
\ No newline at end of file