https://github.com/thomaslevesque/Hamlet
“To be, or not to be, that is the question”. A simple Option type for .NET
https://github.com/thomaslevesque/Hamlet
Last synced: about 1 year ago
JSON representation
“To be, or not to be, that is the question”. A simple Option type for .NET
- Host: GitHub
- URL: https://github.com/thomaslevesque/Hamlet
- Owner: thomaslevesque
- License: mit
- Created: 2019-02-04T15:25:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-02-10T08:10:54.000Z (over 4 years ago)
- Last Synced: 2025-03-27T11:13:24.555Z (about 1 year ago)
- Language: C#
- Homepage:
- Size: 60.5 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hamlet

[](https://www.nuget.org/packages/Hamlet)
[](https://ci.appveyor.com/project/thomaslevesque/hamlet)
[](https://ci.appveyor.com/project/thomaslevesque/hamlet/build/tests)
> “To be, or not to be, that is the question”
Hamlet is a small .NET library that provides a simple option type, common operations for working with
options (map, bind, filter...), as well as some extension methods for using Linq syntax on options.
An option type is useful to represent the presence or absence of a value. In C#, `null` is often used
for that purpose, but it has some issues:
- it's a frequent source of bugs, because developers often forget to check for null
- `null` might be a valid value in some scenarios, so it doesn't really mean the same thing as an
absence of value; "I have a value that is null" is not the same as "I have no value".
Most functional languages have an option type, often called `Option` (as in F#) or `Maybe` (as in
Haskell). C# could also benefit from such a type, so here it is!
## Usage
### Creating an option
```csharp
using Hamlet;
// An optional int with the value 42
Option a = Option.Some(42);
// or just
Option a = 42;
// An optional int with no value
Option b = Option.None();
// or
var b = Option.None();
// or just
Option b = default;
```
### Checking if a value is present and getting the value
```csharp
Option option = ...
// Explicit check
if (option.IsSome)
{
int value = option.Value;
...
}
// Using TryGetValue
if (option.TryGetValue(out var value))
{
...
}
```
### Usage with Linq
Linq can be used to work with the value that might be contained in an option, by using projections or filters.
In the following example, `result` will be `Some(option.Value + 1)` if `option` has a value, and `None` otherwise:
```csharp
using Hamlet;
using System.Linq;
Option option = ...
Option result =
from value in option
select value + 1;
```
In the following example, `result` will be `Some(option.Value)` if `option` has a value greater than 0, and
`None` otherwise:
```csharp
using Hamlet;
using System.Linq;
Option option = ...
Option result =
from value in option
where value > 0
select value;
```
Values from multiple options can also be combined. In the following example, `result` will be
`Some(optA.Value + optB.Value)` if both `optA` and `optB` have a value, and `optB`'s value is greater than 0:
```csharp
using Hamlet;
using System.Linq;
Option optA = ...
Option optB = ...
Option result =
from a in optA
from b in optB
where b > 0
select a + b;
```
## Credits
The package logo is the icon [Hamlet](https://thenounproject.com/term/hamlet/258402/) by Priscilla Alves from the Noun Project, under license [CC BY 3.0](https://creativecommons.org/licenses/by/3.0/).