MonadTask

The type a chain step returns. Chain, IChain and ShortCircuit return MonadTask<TInput, TReturn>, an awaitable struct that queues each link to run after the ones before it and keeps the fluent surface, so the next call names only the junction type. You rarely name it: it is what makes Chain<A>().Chain<B>().Resolve() compile.

Signature

namespace Trax.Core.Train;
 
public readonly struct MonadTask<TInput, TReturn>
{
    // The chain methods, each returning the next link
    public MonadTask<TInput, TReturn> Chain<TJunction>() where TJunction : class;
    public MonadTask<TInput, TReturn> IChain<TJunction>() where TJunction : class;
    public MonadTask<TInput, TReturn> ShortCircuit<TJunction>() where TJunction : class;
    public MonadTask<TInput, TReturn> Extract<TIn, TOut>();
    public MonadTask<TInput, TReturn> AddServices<T1>(T1 service);  // up to seven services
    public Task<Either<Exception, TReturn>> Resolve();
    // ...plus the instance and explicit-type overloads, as on Train
 
    // Awaiting
    public TaskAwaiter<Monad<TInput, TReturn>> GetAwaiter();
    public ConfiguredTaskAwaitable<Monad<TInput, TReturn>> ConfigureAwait(bool continueOnCapturedContext);
    public Task<Monad<TInput, TReturn>> AsTask();
    public static implicit operator Task<Monad<TInput, TReturn>>(MonadTask<TInput, TReturn> mt);
}

Every member is public: the struct is the receiver of the chain. On Train the methods that start a chain are protected.

Members

MemberDescription
Chain, IChain, ShortCircuit, Extract, AddServicesQueue the next link. See Chain, ShortCircuit, Extract and AddServices. A link is skipped if an earlier one failed.
Resolve()Ends the chain and returns Task<Either<Exception, TReturn>>, which is what Junctions() returns. See Resolve.
GetAwaiter() / ConfigureAwaitAwait the links queued so far. A failed junction does not throw here; the failure stays in the monad until Resolve().
AsTask() / implicit conversionThe task behind the chain, for APIs that take a Task<T>.

Remarks

  • Junctions() is a declaration: return the chain, ending in Resolve(). Awaiting a MonadTask inside Junctions() is work, not declaration, and the startup chain check refuses a Junctions() that awaits before returning.
  • Monad<TInput, TReturn> (namespace Trax.Core.Monad) is what Extract and AddServices return when called directly on the train. It has the same chain methods, and its Chain returns a MonadTask. It has no public constructor.

Package

dotnet add package Trax.Core