Skip to content

Typestate Protocol

github-actions[bot] edited this page Nov 30, 2026 · 5 revisions

Typestate Protocol (Ferrous.States)

Ferrous.States pairs with Ferrous.Ownership to enforce linear state machines at compile time using [Typestate] (IsInitial, IsTerminal) and [ConsumesSelf].


Example: Payment Intent Lifecycle

In vanilla .NET, stateful workflows (HttpRequestBuilder, DbTransaction, PaymentIntent) usually enforce call ordering with a runtime enum State field and InvalidOperationException—meaning out-of-order calls, double transitions, or forgotten finalization all compile cleanly and only fail at runtime:

// Vanilla C#: illegal call sequences compile and fail at runtime
public sealed class PaymentIntent
{
    private string _state = "Created";

    public void Authorize() { if (_state != "Created") throw new InvalidOperationException(); _state = "Authorized"; }
    public void Capture()   { if (_state != "Authorized") throw new InvalidOperationException(); _state = "Captured"; }
}

var intent = new PaymentIntent();
intent.Capture();   // Compiles, throws InvalidOperationException at runtime
intent.Authorize();
intent.Authorize(); // Compiles, double-authorization bug

With Ferrous.States + Ferrous.Ownership, each state is its own type, each transition consumes the previous state ([ConsumesSelf]), and valid end states are marked [Typestate(IsTerminal = true)]:

using Ferrous;

[Typestate(IsInitial = true)]
public sealed class DraftPayment
{
    [ConsumesSelf]
    public AuthorizedPayment Authorize() => new AuthorizedPayment();
}

[Typestate]
public sealed class AuthorizedPayment
{
    [ConsumesSelf]
    public CapturedPayment Capture() => new CapturedPayment();

    [ConsumesSelf]
    public VoidedPayment Void() => new VoidedPayment();
}

[Typestate(IsTerminal = true)]
public sealed class CapturedPayment { }

[Typestate(IsTerminal = true)]
public sealed class VoidedPayment { }

Now every protocol bug becomes a compile-time error:

  1. Skipping states (new AuthorizedPayment()): blocked by FE0300.
  2. Double-capturing or using stale state (auth.Capture(); auth.Void();): blocked by FE0200 (use of moved value auth).
  3. Forgetting to finish an authorized payment (falling out of scope on a branch without calling Capture() or Void()): caught by FE0303 (leaked non-terminal typestate)!
  4. Visualizing & auditing the state machine: dotnet fe graph src/ renders a Mermaid stateDiagram-v2 with [*] --> DraftPayment, CapturedPayment --> [*], and flags unreachable or non-terminal dead-end states.

Generated Mermaid Diagram (dotnet fe graph)

Running dotnet fe graph src/ on the payment state machine above emits (and GitHub/static docs render):

stateDiagram-v2
    [*] --> DraftPayment
    DraftPayment --> AuthorizedPayment : Authorize()
    AuthorizedPayment --> CapturedPayment : Capture()
    AuthorizedPayment --> VoidedPayment : Void()
    CapturedPayment --> [*]
    VoidedPayment --> [*]
Loading

You can also keep a diagram in any Markdown file automatically up to date in CI with dotnet fe graph src/ --write docs/Architecture.md, or get it embedded automatically in your static API docs via dotnet fe doc.


Diagnostics

ID Severity Description
FE0300 Error Direct construction of a non-initial [Typestate] outside a [ConsumesSelf] transition.
FE0301 Warning Instance transition method between [Typestate] types missing [ConsumesSelf] (IDE quick-fix available via Ferrous.CodeFixes).
FE0302 Warning Resulting typestate from a [ConsumesSelf] transition was discarded.
FE0303 Warning Non-terminal [Typestate] local falls out of scope without transitioning ([ConsumesSelf]), being consumed ([Consuming]), or being returned.

Clone this wiki locally