https://github.com/ricardotondello/functional.toolkit.optiontype
Functional.Toolkit.OptionType is a lightweight library that provides an implementation of the Option type in C#. The Option type is a functional programming concept that represents a value that may or may not be present. This can help reduce the number of null checks in your code, making it more robust and easier to reason about.
https://github.com/ricardotondello/functional.toolkit.optiontype
csharp dotnet dotnetcore extensions option-type
Last synced: 9 months ago
JSON representation
Functional.Toolkit.OptionType is a lightweight library that provides an implementation of the Option type in C#. The Option type is a functional programming concept that represents a value that may or may not be present. This can help reduce the number of null checks in your code, making it more robust and easier to reason about.
- Host: GitHub
- URL: https://github.com/ricardotondello/functional.toolkit.optiontype
- Owner: ricardotondello
- License: mit
- Created: 2023-05-11T07:30:59.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-01-16T15:32:19.000Z (over 1 year ago)
- Last Synced: 2025-01-16T16:58:57.688Z (over 1 year ago)
- Topics: csharp, dotnet, dotnetcore, extensions, option-type
- Language: C#
- Homepage:
- Size: 356 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# 🚀 Functional.Toolkit.OptionType
[](https://github.com/ricardotondello/Functional.Toolkit.OptionType/actions/workflows/dotnet.yml)
[](https://sonarcloud.io/dashboard?id=ricardotondello_Functional.Toolkit.OptionType)
[](https://sonarcloud.io/component_measures?id=ricardotondello_Functional.Toolkit.OptionType&metric=coverage)
[](https://nuget.org/packages/Functional.Toolkit.OptionType)
[](https://www.nuget.org/packages/Functional.Toolkit.OptionType)
Functional.Toolkit.OptionType is a lightweight library that provides an implementation of the Option type in C#.
The Option type is a functional programming concept that represents a value that may or may not be present.
This can help reduce the number of null checks in your code, making it more robust and easier to reason about.
_Here its just a few examples, check all the available extensions in the [OptionExtensions.cs](https://github.com/ricardotondello/Functional.Toolkit.OptionType/blob/main/src/Functional.Toolkit.OptionType/OptionExtensions.cs) file_
## Usage
```csharp
//Object Initialization
var option1 = Option.From((ClassForTest)null); //will automatically determine if its None or Some based on the generic type
var option2 = Option.From(new ClassForTest());
var option3 = Option.None();
var option4 = Option.Some(new ClassForTest());
...
//Sync
var maybeClass = await MaybeGetClass();
maybeClass
.WhenSome(value =>
{
Console.WriteLine($"Proceed doing something with the value: {value.Name}");
//It could be whatever you want, here you have the value of the type of the Option
//Example:
//Call another methods, write in the DB, Publish to a Service Bus, infinity...
})
.WhenNone(() =>
{
Console.WriteLine("Proceed as a fallback without the value");
//since the Option is None the value couldn't be found.
//Example:
//Write logs, alerts, whatever.
})
.WhenAny(maybeValue =>
{
Console.WriteLine("Despite having a value or not this will be execute and you can decide what to do.");
Console.WriteLine($"Does my object has a value? {maybeValue.HasValue}");
});
//Async
var maybeClassTask = MaybeGetClass();
var result = await maybeClassTask
.WhenSomeAsync(value =>
{
Console.WriteLine($"Proceed doing something with the value: {value.Name}");
return Task.CompletedTask;
})
.WhenNoneAsync(() =>
{
Console.WriteLine("Proceed as a fallback without the value");
return Task.CompletedTask;
}).WhenAnyAsync(maybeValue =>
{
Console.WriteLine($"Does my object has a value? {maybeValue.HasValue}");
});
```
## Contributing
Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub.
If you would like to contribute code, please fork the repository and submit a pull request.
## License
Functional.Toolkit.OptionType is licensed under the MIT License.
See [LICENSE](https://github.com/ricardotondello/Functional.Toolkit.OptionType/blob/main/LICENSE) for more information.
## Support
