Purview.Unions
1.0.0-prerelease.3
dotnet add package Purview.Unions --version 1.0.0-prerelease.3
NuGet\Install-Package Purview.Unions -Version 1.0.0-prerelease.3
<PackageReference Include="Purview.Unions" Version="1.0.0-prerelease.3" />
<PackageVersion Include="Purview.Unions" Version="1.0.0-prerelease.3" />
<PackageReference Include="Purview.Unions" />
paket add Purview.Unions --version 1.0.0-prerelease.3
#r "nuget: Purview.Unions, 1.0.0-prerelease.3"
#:package Purview.Unions@1.0.0-prerelease.3
#addin nuget:?package=Purview.Unions&version=1.0.0-prerelease.3&prerelease
#tool nuget:?package=Purview.Unions&version=1.0.0-prerelease.3&prerelease
Purview.Unions
Dependency-free C# 15 unions that do not box value-type cases. The union keyword stores one
object?, so every value-type case is boxed. This package generates the language's custom union type
instead: a typed field per case, a public constructor per case, and a non-boxing TryGetValue. The compiler
still applies union conversions, exhaustive switch and is/null patterns.
Install
dotnet add package Purview.Unions
Quick start
using Purview.Unions;
[GenerateUnion(typeof(int), typeof(string))]
public readonly partial struct IntOrString;
IntOrString value = 42;
Console.WriteLine(value switch
{
int number => $"int {number}",
string text => $"string {text}",
null => "empty",
});
The package carries the merged generator and code fixes under analyzers/dotnet/cs, so no second
PackageReference is needed.
Declaration surfaces
| Surface | Shape |
|---|---|
| A | [GenerateUnion(typeof(int), typeof(string))] public readonly partial struct IntOrString; |
| B | [GenerateUnion] public readonly partial struct Option<T> { internal static partial Option<T> Some(Some<T> value); } |
| C | a [GenerateUnion] union implementing its own nested IUnionMembers provider |
| D | Purview.Unions.Union<int, string> value = 42; |
Use B or C for a generic union: typeof(T) is not a legal attribute argument (CS0416).
Boxing
| Operation | Allocates? |
|---|---|
union conversion, TryGetValue, is, switch, is null |
No |
reading Value |
Yes, for a value-type case |
Value is part of the language's union contract and boxes. That is inherent to the contract; pattern
matching does not use it.
Requirements
- .NET 11 SDK (C# 15, GA).
net10.0ornet11.0. Anet11.0project gets C# 15 by default and needs noLangVersion; anet10.0project sets<LangVersion>latest</LangVersion>because its default is C# 14. Onnet10.0the generator emits internal[Embedded]polyfills;net11.0uses the BCL types and never receives a duplicate.- Do not set
EnablePreviewFeatures;LangVersion=latest(only onnet10.0) is enough.
Supported declarations
struct, readonly struct, class, record struct and record class, non-generic and generic, with
classes, structs, records, interfaces, type parameters, nullable value types and other unions as cases.
A nullable value-type case is exposed through its underlying type: int? becomes case int, the constructor
takes int?, and TryGetValue(out int) returns false for null.
Diagnostics
UNI0001–UNI0016 and UNI9000. See the
Diagnostics page. Code fixes add a
missing partial (UNI0001), make an inaccessible case type public (UNI0005) and, on the fallback path, the
[Union] attribute (UNI0007). UNI0000, UNI9001 and UNI9002 are suppression descriptors, not rules:
they suppress CA1034, CA1515 and CA1000 on a union's nested IUnionMembers provider.
Switches
| Property | Default | Purpose |
|---|---|---|
DisableUnionsSourceGenerator |
false |
Disables generation and the polyfills |
Agent skills
The purview-unions-quickstart, purview-unions-modelling, purview-unions-diagnostics and
purview-unions-interop agent skills cover this package. No package ships skills: install them from the git
repository with the skills CLI:
npx skills add purview-dev/unions
Licence
MIT.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. net11.0 is compatible. |
-
net10.0
- No dependencies.
-
net11.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-prerelease.3 | 48 | 11/12/2026 |
| 1.0.0-prerelease.2 | 44 | 11/12/2026 |
| 1.0.0-prerelease.1 | 49 | 11/11/2026 |