Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/whiteblackgoose/csharpoption
Success/Maybe/Option/etc. type for C#, easy to use and cheap and etc.
https://github.com/whiteblackgoose/csharpoption
Last synced: 18 days ago
JSON representation
Success/Maybe/Option/etc. type for C#, easy to use and cheap and etc.
- Host: GitHub
- URL: https://github.com/whiteblackgoose/csharpoption
- Owner: WhiteBlackGoose
- License: cc0-1.0
- Created: 2021-06-02T19:53:04.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-06-12T09:33:49.000Z (over 3 years ago)
- Last Synced: 2024-10-19T18:47:12.458Z (30 days ago)
- Language: C#
- Size: 9.77 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> **WARNING**: Deprecated.
>
> Use [Honk#](https://github.com/WhiteBlackGoose/HonkSharp) instead.## Option for C\#
Imagine using the `TryPattern` in 2021... compare this old-fashioned ugly bruuh
```cs
if (!...TryDo(..., out var res))
return "Invalid";
if (res.Property > 0 and res.Property < 6)
return res.ToString();
else
return "Quack";
```
to this nice cool awesome
```cs
return Do(...) switch
{
Success({ Property: > 0 and < 6 } res) => res.ToString(),
Success => "Quack",
_ => "Invalid"
};
```### TryParse
With
```cs
static Success? Parse(string? s)
=> int.TryParse(s, out var res) ? new(res) : null;
```
You now can write
```cs
Console.WriteLine(
Parse(Console.WriteLine()) switch
{
Success(> 6) => $"Value is valid and greater than 6",
Success => "Value is valid",
var Failure => "Invalid value"
}
);
```### TryGet
Also, you can now use `null` as a valid type. With
```cs
public static class Extensions
{
public static Success? Get(this Dictionary dic, TKey key)
=> dic.TryGetValue(key, out var res) ? new(res) : null;
}
```
You can now write
```cs
var dict = new Dictionary { { "key", null } };
Console.WriteLine(
dict.Get("key") switch
{
Success(null) => "Got a valid null",
Success(var notNull) => $"Got a valid notnull: {notNull}",
_ => "Got invalid"
}
);
```