Table of Contents

Enum TrackBehavior

Namespace
FunctionalDdd
Assembly
FunctionalDdd.RailwayOrientedProgramming.dll

Defines which railway track an operation runs on in Railway-Oriented Programming.

public enum TrackBehavior
Extension Methods

Fields

Both = 2

Operation executes on both success and failure tracks. Processes the Result regardless of its state.

Universal operations include:

  • Combine - Merge multiple results (aggregates both successes and failures)

Example:

firstName.Combine(lastName)  // Combines results regardless of individual success/failure
         .Combine(email);     // Aggregates all errors if any fail
Failure = 1

Operation only executes when Result is a failure. Skipped if the Result is successful.

Failure track operations include:

  • TapOnFailure - Execute side effects on errors
  • MapOnFailure - Transform errors
  • RecoverOnFailure - Attempt recovery from errors

Example:

result.TapOnFailure(err => LogError(err))      // Only runs if result failed
      .MapOnFailure(err => AddContext(err))     // Only runs if still failed
      .RecoverOnFailure(() => GetDefault());    // Attempts recovery if failed
Success = 0

Operation only executes when Result is successful. Skipped if the Result is a failure.

Success track operations include:

  • Bind - Chain operations that can fail
  • Map - Transform success values
  • Tap - Execute side effects on success
  • Ensure - Validate conditions (can switch to failure)

Example:

result.Bind(user => GetOrders(user))  // Only runs if result is success
      .Map(orders => orders.Count)     // Only runs if Bind succeeded
      .Tap(count => Log(count));       // Only runs if Map succeeded
Terminal = 3

Terminal operation that handles both tracks and exits the railway. Unwraps the Result and produces a final value.

Terminal operations include:

  • Match - Pattern match on success/failure
  • MatchError - Pattern match on specific error types

Example:

result.Match(
    onSuccess: user => Ok(user),
    onFailure: error => BadRequest(error.Detail)
);  // Exits the railway and returns final value

Remarks

In Railway-Oriented Programming, operations flow along two tracks:

  • Success Track - Operations continue when everything works correctly
  • Failure Track - Errors are captured and propagated automatically

Understanding track behavior is essential for composing railway-oriented pipelines. This enum makes the behavior explicit and enables tooling support.