Skip to content
A library design for programming with effects and handlers in C# 7
C# Shell Dockerfile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode
samples Simplify naming of awaiter classes (#17) Feb 13, 2020
src Simplify naming of awaiter classes (#17) Feb 13, 2020
tests
.dockerignore infra fixes Jan 22, 2020
.gitattributes Init src structure. May 2, 2017
.gitignore infra fixes Jan 22, 2020
.travis.yml enable CI Oct 31, 2019
Directory.Build.props enable CI Oct 31, 2019
Dockerfile infra fixes Jan 22, 2020
Eff.code-workspace
Eff.sln
LICENSE Initial commit May 2, 2017
README.md Update README.md Feb 14, 2020
docker-build.sh infra fixes Jan 22, 2020
nuget-push.sh fix command line switch Jan 22, 2020

README.md

Eff

Nuget Build Status License GitHub language count GitHub top language

A library for programming with effects and handlers in C#, inspired by the Eff programming language and the implementation of Algebraic Effects in OCaml, Eff Directly in OCaml. Effects are a powerful language feature that can be used to implement dependency injection, exception handling, nondeterministic computation, trace logging and much more.

Introduction

The Eff library takes advantage of the async method extensibility features available since C# 7. At its core, the library defines a task-like type, Eff<TResult>, which can be built using async methods:

async Eff HelloWorld()
{
    Console.WriteLine($"Hello, {await Helper()}!");

    async Eff<string> Helper() => "World";
}

Note that unlike Task, Eff types have cold semantics and so running

Eff hello = HelloWorld();

will have no observable side-effect in stdout. An Eff instance has to be run explicitly by passing an effect handler:

hello.Run(new DefaultEffectHandler()); // "Hello, World!"

So what is the benefit of using a convoluted version of regular async methods?

Programming with Effects

A key concept of the Eff library are abstract effects:

public class CoinToss : Effect<bool>
{

}

Eff methods are capable of consuming abstract effects:

async Eff TossNCoins(int n)
{
    for (int i = 0; i < n; i++)
    {
        bool result = await new CoinToss();
        Console.WriteLine($"Got {(result ? "Heads" : "Tails")}");
    }
}

So how do we run this method now? The answer is we need write an effect handler that interprets the abstract effect:

public class RandomCoinTossHandler : EffectHandler
{
    private readonly Random _random = new Random();

    public override async Task Handle<TResult>(EffectAwaiter<TResult> awaiter)
    {
        switch (awaiter)
        {
            case EffectAwaiter<bool> { Effect: CoinToss _ } awtr:
                awtr.SetResult(_random.NextDouble() < 0.5);
                break;
        }
    }
}

We can then execute the method by passing the handler:

TossNCoins(100).Run(new RandomCoinTossHandler()); // prints random sequence of Heads and Tails

Note that we can reuse the same method using other interpretations of the effect:

public class BiasedCoinTossHandler : EffectHandler
{
    private readonly Random _random = new Random();

    public override async Task Handle<TResult>(EffectAwaiter<TResult> awaiter)
    {
        switch (awaiter)
        {
            case EffectAwaiter<bool> { Effect: CoinToss _ } awtr:
                awtr.SetResult(_random.NextDouble() < 0.01);
                break;
        }
    }
}

TossNCoins(100).Run(new BiasedCoinTossHandler()); // prints sequence of mostly Tails

Please see the samples folder for more examples of Eff applications.

You can’t perform that action at this time.