Withat 0.42.1
dotnet add package Withat --version 0.42.1
NuGet\Install-Package Withat -Version 0.42.1
<PackageReference Include="Withat" Version="0.42.1" />
<PackageVersion Include="Withat" Version="0.42.1" />
<PackageReference Include="Withat" />
paket add Withat --version 0.42.1
#r "nuget: Withat, 0.42.1"
#:package Withat@0.42.1
#addin nuget:?package=Withat&version=0.42.1
#tool nuget:?package=Withat&version=0.42.1
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
Withmethods for record properties. - Extend immutable collections like
ImmutableArray,ImmutableList, andImmutableDictionarywith 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:
- 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]
- 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]
- 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:
- Report Bugs: If you encounter any issues, please open an issue on GitHub.
- Suggest Features: Have an idea for improvement? Let us know by creating a feature request.
- 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:
- GitHub, Telegram: @xikxekxok
Thank you for using Withat! We hope it makes your development experience smoother and more enjoyable. 🚀
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.0)
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 |