The Readme generated by LLM with minimum human editing. Be patient
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.
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.
// 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 = 60All With methods have asynchronous variants, making it easy to integrate with async workflows.
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");You can ignore specific properties from being extended by applying the [ExtendedWithIgnore] attribute.
[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
);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.
- 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}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.
- Follow the existing coding style.
- Write unit tests for new features.
- Ensure all tests pass before submitting a PR.
This project is licensed under the MIT License.
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. 🚀