Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/badeend/nothing
If you want to use `void` as a type parameter, but C# won't let you...
https://github.com/badeend/nothing
csharp functional-programming hack unit
Last synced: about 1 month ago
JSON representation
If you want to use `void` as a type parameter, but C# won't let you...
- Host: GitHub
- URL: https://github.com/badeend/nothing
- Owner: badeend
- License: mit
- Created: 2024-04-14T12:25:03.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-12-26T11:15:59.000Z (about 2 months ago)
- Last Synced: 2025-01-08T12:49:29.975Z (about 1 month ago)
- Topics: csharp, functional-programming, hack, unit
- Language: C#
- Homepage:
- Size: 38.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
![]()
If you want to use `void` as a type parameter, but C# won't let you...---
`Badeend.Nothing` is struct that contains... well... nothing!
Also known as a "unit" type in Functional Programming parlance.
In C# this can be used in places where you have nothing to return, but `void` is not allowed. Most notably: generics.
#### Example time!
Let's assume there exists an interface that you want to implement:
```cs
public interface IHandler
{
T Handle(MyInput input);
}
```If your handler has nothing useful to return, then you would _want_ to write this:
```cs
public class MyHandler : IHandler // COMPILE ERROR!
{
void Handle(MyInput input)
{
// (my implementation here)
}
}
```Unfortunately, "Computer Says No". C# won't let you.
If you're lucky enough to own that interface, you could decide to duplicate the interface and all related code: one for `IHandler`, and one for `IHandler`.
If you're not able to change the interface or just don't feel like doing a bunch of unnecessary work; you could also change the type argument to `Nothing`:
```cs
using Badeend;public class MyHandler : IHandler // <--- Nothing to see here
{
Nothing Handle(MyInput input)
{
// (my implementation here)return Nothing.Value;
}
}
```## Installation
[![NuGet Badeend.Nothing](https://img.shields.io/nuget/v/Badeend.Nothing?label=Badeend.Nothing)](https://www.nuget.org/packages/Badeend.Nothing)
```sh
dotnet add package Badeend.Nothing
```## Fake `nothing` keyword
Depending on how intensively you use this package, you might be interested in using `Badeend.Nothing.Keyword` globally, which exposes `nothing` as a top level field.
```cs
using Badeend;public class MyHandler : IHandler
{
Nothing Handle(MyInput input)
{
// (my implementation here)return nothing; // <--- Note the lowercase 'n'
}
}
```In [C#10+](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#global-modifier) this can be configured as follows:
```cs
global using static Badeend.Nothing.Keyword;
```or:
```xml
```
---
### Shameless self-promotion
May I interest you in one of my other packages?
- **[Badeend.ValueCollections](https://badeend.github.io/ValueCollections/)**: _Low overhead immutable collection types with structural equality._
- **[Badeend.EnumClass](https://badeend.github.io/EnumClass/)**: _Discriminated unions for C# with exhaustiveness checking._
- **[Badeend.Result](https://badeend.github.io/Result/)**: _For failures that are not exceptional: `Result` for C#._
- **[Badeend.Any](https://badeend.github.io/Any/)**: _Holds any value of any type, without boxing small structs (up to 8 bytes)._
- **[Badeend.Nothing](https://github.com/badeend/Nothing)**: _If you want to use `void` as a type parameter, but C# won't let you._