Skip to main content

Aigamo.MatchGenerator by Aigamo

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: Aigamo.MatchGenerator

Package Description

Author: Aigamo

NuGet: https://www.nuget.org/packages/Aigamo.MatchGenerator/

You can find more details at https://github.com/ycanardeau/MatchGenerator

Source: https://github.com/ycanardeau/MatchGenerator

Author

note

Aigamo Alt text

Original Readme

note

MatchGenerator

Bring exhaustive pattern matching to C# enums and unions with zero boilerplate.

MatchGenerator is a Roslyn source generator that creates Match extension methods for your enums and discriminated-union-like types, enabling concise, expressive, and compile-time safe branching.

Features
  • Generate Match extension methods for enums and unions
  • Exhaustive by design (no missing cases)
  • Attribute-driven (opt-in per type)
  • Supports generics (Match<U>)
  • Respects effective accessibility
  • Zero runtime cost (pure source generation)
Getting Started

######### 1. Install the package

dotnet add package Aigamo.MatchGenerator

######### 2. Annotate your type

########## Enum example

using Aigamo.MatchGenerator;

[GenerateMatch]
public enum Gender
{
Male = 1,
Female,
}

########## Union example

using Aigamo.MatchGenerator;

[GenerateMatch]
abstract record MaritalStatus;

sealed record Single : MaritalStatus;
sealed record Married : MaritalStatus;
sealed record Divorced : MaritalStatus;
sealed record Widowed : MaritalStatus;

######### 3. Use Match

########## Enum

var message = gender.Match(
onMale: () => "male",
onFemale: () => "female"
);

########## Union

var message = maritalStatus.Match(
onSingle: x => "single",
onMarried: x => "married",
onDivorced: x => "divorced",
onWidowed: x => "widowed"
);
Why use MatchGenerator?

######### Without MatchGenerator

########## Enum

var message = gender switch
{
Gender.Male => "male",
Gender.Female => "female",
_ => throw new UnreachableException(),
};

########## Union

var message = maritalStatus switch
{
Single x => "single",
Married x => "married",
Divorced x => "divorced",
Widowed x => "widowed",
_ => throw new UnreachableException(),
};

######### With MatchGenerator

var message = gender.Match(
onMale: () => "male",
onFemale: () => "female"
);
  • More concise
  • More readable
  • No default case required
  • Compile-time safety
Exhaustiveness Guarantee

All cases must be handled.

If a new enum value or union type is added:

public enum Gender
{
Male = 1,
Female,
Other,
}

or

sealed record Separated : MaritalStatus;

Existing Match calls will fail to compile until updated. This ensures no cases are missed.

Generated Code (Example)

######### Enum

internal static class GenderMatchExtensions
{
public static U Match<U>(
this Gender value,
Func<U> onFemale,
Func<U> onMale
)
{
return value switch
{
Gender.Female => onFemale(),
Gender.Male => onMale(),
_ => throw new UnreachableException(),
};
}
}

######### Union

internal static class MaritalStatusMatchExtensions
{
public static U Match<U>(
this MaritalStatus value,
Func<Divorced, U> onDivorced,
Func<Married, U> onMarried,
Func<Single, U> onSingle,
Func<Widowed, U> onWidowed
)
{
return value switch
{
Divorced x => onDivorced(x),
Married x => onMarried(x),
Single x => onSingle(x),
Widowed x => onWidowed(x),
_ => throw new UnreachableException(),
};
}
}
References

About

note

Transform enums into tagged unions -with error in Match !

How to use

Example (source csproj, source files)

This is the CSharp Project that references Aigamo.MatchGenerator

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aigamo.MatchGenerator" Version="0.0.0-preview013" />
</ItemGroup>




</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated />
using System;
using System.Diagnostics;

namespace EnumDemo;

public static class CarTypesMatchExtensions
{
public static U Match<U>(
this CarTypes value,
Func<U> onBMW,
Func<U> onDacia,
Func<U> onMercedes,
Func<U> onNone,
Func<U> onTesla
)
{
return value switch
{
CarTypes.BMW => onBMW(),
CarTypes.Dacia => onDacia(),
CarTypes.Mercedes => onMercedes(),
CarTypes.None => onNone(),
CarTypes.Tesla => onTesla(),
_ => throw new UnreachableException(),
};
}
}

Useful

Download Example (.NET C#)

Share Aigamo.MatchGenerator

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Aigamo.MatchGenerator

Category "Enum" has the following generators:

1 Aigamo.MatchGenerator Nuget GitHub Repo stars 2026-04-05

2 CredFetoEnum Nuget GitHub Repo stars 2023-10-12

3 EnumClass Nuget GitHub Repo stars 2023-08-08

4 EnumsEnhanced Nuget GitHub Repo stars 2025-08-05

5 EnumUtilities Nuget GitHub Repo stars 2024-04-05

6 Flaggen Nuget GitHub Repo stars 2025-07-23

7 FusionReactor Nuget GitHub Repo stars 2024-04-06

8 Genbox.FastEnum Nuget GitHub Repo stars 2025-08-03

9 jos.enumeration Nuget GitHub Repo stars 2025-07-20

10 NetEscapades.EnumGenerators Nuget GitHub Repo stars 2023-04-16

11 PMart.Enumeration NugetNuget GitHub Repo stars 2025-03-25

12 RapidEnum Nuget GitHub Repo stars 2025-10-04

13 requiredenum Nuget GitHub Repo stars 2025-08-14

14 TaggedEnum Nuget GitHub Repo stars 2026-04-05

See category

Enum