Rascal 1.0.0-pre
See the version list below for details.
dotnet add package Rascal --version 1.0.0-pre
NuGet\Install-Package Rascal -Version 1.0.0-pre
<PackageReference Include="Rascal" Version="1.0.0-pre" />
<PackageVersion Include="Rascal" Version="1.0.0-pre" />
<PackageReference Include="Rascal" />
paket add Rascal --version 1.0.0-pre
#r "nuget: Rascal, 1.0.0-pre"
#:package Rascal@1.0.0-pre
#addin nuget:?package=Rascal&version=1.0.0-pre&prerelease
#tool nuget:?package=Rascal&version=1.0.0-pre&prerelease
Rascal
Rascal is a simple and lightweight result type implementation for C#, containing a variety of utilities and standard functions for working with result types and integrating them into the rest of C#.
Rascal is first and foremost an aggregate of the result types I personally find myself implementing in a majority of my own projects, and a competetor other result libraries second. As such, this library implements some things I think other result implementations are lacking, while omitting some features other libraries do implement.
The prelude
Rascal contains a Prelude
class (named in reference to most functional languages) which contains a wide variety of utility functions. Since this class contains functions which are used very frequently in code heavily utilizing results, this class is meant to be imported statically, i.e. through a using static
statement. For convenience, this can be included as a global using in a Usings.cs
file containing other global using statements.
Samples
Creating a result
// Through explicit Ok/Error functions
var explicitOk = Ok("uwu");
var explicitErr = Err<int>("An error occured.");
// Through implicit conversion
Result<string> implicitOk = "owo";
Mapping a result
"Mapping" refers to taking a result containing some value of type T
and mapping said value to a value of some other type TNew
.
// Read input, parse to int, and apply a function to the value
var x = ParseR<int>(Console.ReadLine()!)
.Map(x => Enumerable.Repeat("hewwo", x));
Another operation, quite similar to mapping, exists, known as a "bind". A bind operation acts like a map, but the function applied to the value of type T
returns another result, namely a Result<TNew>
, which is then returned from the bind. This is the fundamental mechanism which allows chaining result operations together, making for a quite powerful tool.
// Read input, parse to int, and apply a function to the value, which may fail
var num = ParseR<int>(Console.ReadLine()!);
var den = ParseR<int>(Console.ReadLine()!);
var val = num.Then(a => den
.Map(b => DiveSafe(a, b)));
static Result<int> DivSafe(int a, int b) =>
b != 0
? a / b
: "Cannot divide by 0.";
The above expression for val
can alternatively be written using query syntax:
var val =
from a in num
from b in den
from x in DivSafe(a, b)
select x;
Various utilities
Parse a string or ReadOnlySpan<char>
to another type, returning a result. ParseR
(short for ParseResult
) works for any type implementing IParsable<TSelf>
or ISpanParsable<TSelf>
.
var parsed = ParseR<int>(Console.ReadLine()!);
Turn a nullable value into a result.
var result = F().NotNull();
static int? F();
A function for running another function in a try
block and returning a result containing either the successful value of the function or the thrown exception. Quite useful for functions which provide no good way of checking whether success is expected before running it, such as IO. Try
variants are also available for Map
and Then
.
var result = Try(() => File.ReadAllText(path));
Validate inputs directly inside a result expression chain, replacing the original value with an error if the predicate fails.
var input = ParseR<int>(Console.ReadLine()!)
.Validate(
x => x >= 0,
x => $"Input {x} is less than 0.")
// can also be written as
var input =
from x in ParseR<int>(Console.ReadLine()!)
where x >= 0
select x;
"Unsafe" operations
To not be too far out-of-line with the rest of C#, there are also functions for accessing the values inside results in an unsafe manner. "Unsafe" in this context is not referring to the unsafe
from C#, but rather the fact these functions may throw exceptions, as opposed to most other functions which are pure and should not normally throw exceptions. These functions should be treated with care and only be used in situations where the caller knows without a reasonable shadow of a doubt that the operation is safe or an exception is acceptable to be thrown.
Result<int> result;
int x = result.Unwrap();
// or
int y = (int)result;
int z = result.Expect("Expected result to be successful.");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on Rascal:
Repository | Stars |
---|---|
jscarle/LightResults
An extremely light and modern Operation Result Pattern library for .NET.
|