Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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...

Awesome Lists containing this project

README

        


Nothing


If you want to use `void` as a type parameter, but C# won't let you...


Nuget

---

`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._