Withat 0.42.1

dotnet add package Withat --version 0.42.1
                    
NuGet\Install-Package Withat -Version 0.42.1
                    
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="Withat" Version="0.42.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Withat" Version="0.42.1" />
                    
Directory.Packages.props
<PackageReference Include="Withat" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Withat --version 0.42.1
                    
#r "nuget: Withat, 0.42.1"
                    
#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.
#:package Withat@0.42.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Withat&version=0.42.1
                    
Install as a Cake Addin
#tool nuget:?package=Withat&version=0.42.1
                    
Install as a Cake Tool

Withat

The Readme generated by LLM with minimum human editing. Be patient

Overview

A source code generator, which provides a set of powerful extension methods that allow developers to create updated copies of immutable records with minimal boilerplate code.

With Withat, you can:

  • Generate With methods for record properties.
  • Extend immutable collections like ImmutableArray, ImmutableList, and ImmutableDictionary with intuitive update methods.
  • Ignore specific properties from being extended using attributes.

Key Features and Usage

Automatic With Method Generation

The library automatically generates With methods for all properties of your records. These methods allow you to create updated copies of immutable objects in a concise and expressive way.

Example:
// Define your record
[ExtendedWith]
public record Person(string Name, int Age);

// Create and update instances
var person = new Person("Alice", 30);
var updatedPerson = person.WithName("Bob"); // Creates a new Person with Name = "Bob"
var updatedPerson2 = person.WithAge(x=>x*2); // Creates a new Person with Age = 60

Async Support

All With methods have asynchronous variants, making it easy to integrate with async workflows.

Example:
var updatedPerson = await person.WithAge(Task.FromResult(31));

var updatedPerson2 = await person.WithAge(async x => x + 5);

var updatedPerson3 = await person
    .WithAge(async x => x + 5)
    .WithName("John Doe");

Customizable Property Handling

You can ignore specific properties from being extended by applying the [ExtendedWithIgnore] attribute.

Example:
[ExtendedWith]
public record Person
{
    public required string Name {get; init;}
    [ExtendedWithIgnore]
    public required int Age {get; init;} // This property will not have a `With` method
);

Immutable Collection Extensions

Withat extends immutable collections such as ImmutableArray, ImmutableList, and ImmutableDictionary with methods like With, WithFirst, WithFirstOrDefault, WithAll, and more. These methods make it easy to update elements within immutable collections.

Examples:
  1. ImmutableArray
var array = ImmutableArray.Create(1, 2, 3);

// Update element at index 1
var updatedArray = array.With(index: 1, valueFunc: x => x * 2); // [1, 4, 3]

// Update first element greater than 1
var updatedArray2 = array.WithFirst(x => x > 1, x => x * 2); // [1, 4, 3]

// Update all elements greater than 1
var updatedArray3 = array.WithAll(x => x > 1, x => x * 2); // [1, 4, 6]
  1. ImmutableList
var list = ImmutableList.Create(1, 2, 3);

// Update element at index 1
var updatedList = list.With(index: 1, valueFunc: x => x * 2); // [1, 4, 3]

// Update first element greater than 1
var updatedList2 = list.WithFirstOrDefault(x => x > 1, x => x * 2); // [1, 4, 3]

// Update all elements greater than 1
var updatedList3 = list.WithAll(x => x > 1, x => x * 2); // [1, 4, 6]
  1. ImmutableDictionary
var dictionary = ImmutableDictionary<string, int>.Empty.Add("A", 1).Add("B", 2);

// Update value for key "A"
var updatedDictionary = dictionary.With("A", x => x + 10); // {"A": 11, "B": 2}

// Update value for key "B" asynchronously
var updatedDictionaryAsync = await dictionary.With("B", async x => x + await Task.FromResult(20)); // {"A": 1, "B": 22}

Contributing

We welcome contributions from the community! Here's how you can help:

  1. Report Bugs: If you encounter any issues, please open an issue on GitHub.
  2. Suggest Features: Have an idea for improvement? Let us know by creating a feature request.
  3. Submit Pull Requests: Fork the repository, make your changes, and submit a pull request.

Development Guidelines

  • Follow the existing coding style.
  • Write unit tests for new features.
  • Ensure all tests pass before submitting a PR.

License

This project is licensed under the MIT License.


Contact

For questions or feedback, feel free to reach out:


Thank you for using Withat! We hope it makes your development experience smoother and more enjoyable. 🚀

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
0.42.1 208 3/13/2025
0.42.0 273 3/7/2025
0.42.0-alpha1 140 2/27/2025