Bnaya.TypePatterns.Generators 1.0.4

dotnet add package Bnaya.TypePatterns.Generators --version 1.0.4                
NuGet\Install-Package Bnaya.TypePatterns.Generators -Version 1.0.4                
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="Bnaya.TypePatterns.Generators" Version="1.0.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Bnaya.TypePatterns.Generators --version 1.0.4                
#r "nuget: Bnaya.TypePatterns.Generators, 1.0.4"                
#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 Bnaya.TypePatterns.Generators as a Cake Addin
#addin nuget:?package=Bnaya.TypePatterns.Generators&version=1.0.4

// Install Bnaya.TypePatterns.Generators as a Cake Tool
#tool nuget:?package=Bnaya.TypePatterns.Generators&version=1.0.4                

TypePatterns Generators

This package show how to mimic Typescropt's Utility Types using Source Code Generator.

[Partial]
public readonly partial record struct RecStruct(int? Value, string Name)
{
    public required int Quantity { get; init; }
    public string Tag { get; init; } = string.Empty;

    public string Description => $"{Name}: {Quantity}";
}

Will generate:

public record struct RecStructNullable
{
	public int? Value{ get; init; }
	public string? Name{ get; init; }
	public int? Quantity{ get; init; }
	public string? Tag{ get; init; }
}

And

partial record struct RecStruct
{
	public static RecStruct operator +(RecStruct a, RecStructNullable b)
	{
		var result = a with {
			Value = b.Value ?? a.Value,
			Name = b.Name ?? a.Name,
			Quantity = b.Quantity ?? a.Quantity,
			Tag = b.Tag ?? a.Tag,
		};
		return result;
	}
}

As result, the following test, pass.

[Fact]
public void RecStruct1_Test()
{
    var r = new RecStruct1 { Value = -150, Name = "Be'ery 😞", Quantity = -150 };
    var rn = new RecStruct1Nullable { Value = -160 };

    var r2 = r + rn;

    Assert.Equal(r.Name, r2.Name);
    Assert.Equal(r.Quantity, r2.Quantity);
    Assert.Equal(rn.Value, r2.Value);
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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.4 448 10/21/2023
1.0.3 233 10/21/2023
1.0.0 283 10/15/2023