DiscriminatedUnion.CS
0.0.6-alpha
This is a prerelease version of DiscriminatedUnion.CS.
There is a newer prerelease version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package DiscriminatedUnion.CS --version 0.0.6-alpha
NuGet\Install-Package DiscriminatedUnion.CS -Version 0.0.6-alpha
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DiscriminatedUnion.CS" Version="0.0.6-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DiscriminatedUnion.CS --version 0.0.6-alpha
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DiscriminatedUnion.CS, 0.0.6-alpha"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DiscriminatedUnion.CS as a Cake Addin #addin nuget:?package=DiscriminatedUnion.CS&version=0.0.6-alpha&prerelease // Install DiscriminatedUnion.CS as a Cake Tool #tool nuget:?package=DiscriminatedUnion.CS&version=0.0.6-alpha&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Discriminated Union
A library that provides functionality to define a Discriminated Union in C#.
Define some types
public class Success<T>
{
public Success(T value)
{
Value = value;
}
public T Value { get; }
}
public class Error
{
public Error(string message)
{
Message = message;
}
public string Message { get; }
}
Define union type
Union type must be an abstract, partial class marked with [GeneratedDiscriminatedUnion]
attribute.
Every implementation of IDiscriminator<T>
defines a discriminator inside the union type.
Where T
is the type that constraints a discriminator.
[GeneratedDiscriminatedUnion]
public abstract partial class Result<T> : IDiscriminator<Success<T>>, IDiscriminator<Error> { }
Use the union type
public class Program
{
public static void Main(string[] args)
{
var result = GetRoot(-1);
var outputMessage = result switch
{
Result<double>.Success s => s.Value.ToString(CultureInfo.InvariantCulture),
Result<double>.Error e => e.Message,
};
Console.WriteLine(outputMessage);
}
public static Result<double> GetRoot(double value)
{
return value switch
{
< 0 => new Error("Value cannot be less than zero"),
_ => new Success<double>(Math.Sqrt(value)),
};
}
}
Limitations
- Union one type multiple times not supported
- T.b.a
There are no supported framework assets in this package.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- DiscriminatedUnion.CS.Annotations (>= 0.0.3-alpha)
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 |
---|---|---|
0.0.8-alpha | 147 | 4/22/2022 |
0.0.7-alpha | 132 | 4/21/2022 |
0.0.6-alpha | 136 | 4/19/2022 |
0.0.5-alpha | 155 | 4/19/2022 |
0.0.4-alpha | 126 | 4/18/2022 |
0.0.3-alpha | 120 | 4/17/2022 |
0.0.2-alpha | 143 | 3/2/2022 |
0.0.1-alpha | 131 | 2/28/2022 |
Added implicit conversions.