From 734d476628f3ee0745d59d67a4d813c2ac45d156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Rab=C3=A9rin?= Date: Sun, 2 Sep 2018 15:28:33 +0200 Subject: [PATCH] Update release notes and README.md. --- src/Here/Here.csproj | 16 +++++++++----- src/Here/Result/README.md | 45 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/Here/Here.csproj b/src/Here/Here.csproj index 1cc54fc..eee00d2 100644 --- a/src/Here/Here.csproj +++ b/src/Here/Here.csproj @@ -22,14 +22,20 @@ Supported platforms: Alexandre Rabérin true Here - ➟ Release 0.4.0 + ➟ Release 0.5.0 - General: - - Update to latest JetBrains annotations package. + - Extend library compatibility until .NET Framework 2.0. + +- For Maybes: + - Add implicit conversion to boolean to handle test like a simple boolean. + - Remove true/false operators (replaced by the implicit conversion to boolean). + - Add extension on string: NoneIfNullOrSpace. + - Add extensions on Maybe<string>: NoneIfEmpty and NoneIfNullOrSpace. - For Results: - - Add extensions OnSuccess. - - Add extensions OnFailure. - - Add extensions OnAny. + - Add implicit conversion to boolean to handle test like a simple boolean. + - Add "Ensure" extensions that allow to check a predicate on each Result type. + - Add cast methods on each Result type to create a Result of another type or with a different value type. Here Functional C# Maybe Monad Result https://opensource.org/licenses/MIT https://github.com/KeRNeLith/Here diff --git a/src/Here/Result/README.md b/src/Here/Result/README.md index 6e10f33..4f7eaa6 100644 --- a/src/Here/Result/README.md +++ b/src/Here/Result/README.md @@ -108,6 +108,18 @@ The necessity to have a message for warnings and errors is motivated by the need Then you have `IResult` and `IResultError` that respectively provide a `Value` and a custom `Error`. +Note that each one support simple conditional test like boolean when you do not want look at extra data, see following example: + +```csharp +Result result = Result.Ok(42); +if (result.IsSuccess) + Console.WriteLine(result.Value); + +// Equivalent to +if (result) + Console.WriteLine(result.Value); +``` + ### Safe Scopes If you want to run code that should return a Result safely, you can use Result scopes to do this. @@ -177,6 +189,39 @@ Database.GetUser("Jack") .OnFailure(() => Console.WriteLine("No user named Jack.")) ``` +### Ensure + +With `Result` (and all results structures) you can run treatments, get the result of it, and then ensure that it meets other requirements. + +Look at the following example (obviously the example is very dummy): + +```csharp +public void Result CheckCountOver(int N) +{ + Result myCount = TreatmentThatCountSomething(); + + // Let assume we only consider the final result success if the count is over N + // So the result will be converted to a failure if the predicate is not matched. + return myCount.Ensure(count => count > N, $"Count must be over {N}."); +} + +``` + +### Cast + +Results support "downcast" implicitly. For example a `Result` can be implicitly converted to a `Result`. But the opposite is not possible. +To do this you can use `Cast` methods that each result type provide. For example if you want to create a `Result` from a simple `Result` you can do like following: + +```csharp +Result res = Result.Ok(); + +Result resDouble = res.Cast(12.5f); +// Or +Result resDouble = res.Cast(() => GetADouble() * 2.5); +``` + +Result with more data inside provide less Cast methods because as said before the implicit conversion is used when you specify the target type. + ### Bridge to Maybe It is possible to convert a `Result`, `Result`, `CustomResult` or `Result` to a `Maybe`.