MalFunction.Result
1.0.0
dotnet add package MalFunction.Result --version 1.0.0
NuGet\Install-Package MalFunction.Result -Version 1.0.0
<PackageReference Include="MalFunction.Result" Version="1.0.0" />
<PackageVersion Include="MalFunction.Result" Version="1.0.0" />
<PackageReference Include="MalFunction.Result" />
paket add MalFunction.Result --version 1.0.0
#r "nuget: MalFunction.Result, 1.0.0"
#:package MalFunction.Result@1.0.0
#addin nuget:?package=MalFunction.Result&version=1.0.0
#tool nuget:?package=MalFunction.Result&version=1.0.0
Malfunction.Result
Getting Started
The result type is an interface that contains two records, Pass and Fail
public interface IResult<TPass, TFail>
{
public sealed record Pass(TPass Value) : IResult<TPass, TFail>;
public sealed record Fail(TFail Value) : IResult<TPass, TFail>;
}
To define a result type, make a type alias. If using it across mulpiple files, define it globally in a seperate file
global using ValidationResult = MalFunction.Result.IResult<int, string>;
Set the return value of your method to your result and return a new pass or fail state
public ValidationResult IsGreaterThan5(int number)
{
if (number > 5)
return new ValidationResult.Pass(number);
else
return new ValidationResult.Fail("Number was not greater than 5");
}
To handle a result, make a type check and handle the success and fail states
string message = IsGreaterThan5(6) switch
{
ValidationResult.Pass pass -> $"Number {pass.Value} was validated",
ValidationResult.Fail fail -> $"Number failed validation with error: {fail.Value}"
};
Console.Writeline(message);
Manipulating Results
Map
Projects result's value into a new form
IResult<int, string> intResult = new IResult<int, string>.Pass(20);
IResult<string, string> stringResult = intResult.Map(x => x.ToString());
Bind
Projects result's value into a new result
IResult<int, string> intResult = new IResult<int, string>.Pass(20);
IResult<string, string> stringResult = intResult.Map(x => new IResult<string, string>.Pass(x.ToString()));
Traverse
Map a result producing function over a list to get a new result
int[] values = [1, 2, 3];
IResult<int, string> NumberMustBeEven()
{
if (x % 2 = 0)
return new IResult<int[], string[]>.Pass(number);
else
return new IResult<int[], string[]>.Fail("Number was odd")
}
IResult<int[], string[]> result = values.Traverse(NumberMustBeEven);
//using fail aggregator
IResult<int[], string> result = values.Traverse(NumberMustBeEven, (error1, error2) => error1 + error2);
//using fail aggregator with projection
record Error(string[] Errors)
IResult<int[], Error> result = results.Traverse(
NumberMustBeEven,
message => new Error([message]),
(error, message) => error.Errors.Append(message)
);
Sequence
Converts a collection of results into a single result containing a collection
IResult<int, string>[] results =
[
new IResult<int, string>.Pass(12),
new IResult<int, string>.Pass(20),
new IResult<int, string>.Fail("error")
];
IResult<int[], string[]> result = results.Sequence();
//using fail aggregator
IResult<int[], string> result = results.Sequence((error1, error2) => error1 + error2);
//using fail aggregator with projection
record Error(string[] Errors)
IResult<int[], Error> result = results.Sequence(
message => new Error([message]),
(error, message) => error.Errors.Append(message)
);
For a detailed explenation on how to use elevated worlds like this result type i recommend you read: https://fsharpforfunandprofit.com/posts/elevated-world/
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0 | 122 | 4/28/2026 |