go-conditions: git.sr.ht/~rauhl/go-conditions Index | Files

package conditions

import "git.sr.ht/~rauhl/go-conditions"

Package conditions implements a Common-Lisp–style condition (or resumable exception) system in Go. It’s a proof-of-concept, not a well-designed, production-ready library.

Among other issues, it’s not possible to return values from functions; state must be manipulated as a side effect.

They do work, though, and perhaps some of the ideas can be adapted.

See https://rauhl.com/2019/06/implementing-a-resumable-exception-system-in-go/ for more information and the impetus for this tremendous abuse of panic, defer & recover.

Index

Package Files

conditions.go debugger.go handlers.go restarts.go signal.go thunks.go

func Error Uses

func Error(ctx context.Context, c Condition)

Error signals that a condition c has occurred. If no handler handles it, then Error will invoke the debugger. It never returns.

func InvokeDebugger Uses

func InvokeDebugger(ctx context.Context, c Condition)

InvokeDebugger invokes the currently-established debugger on the given condition or panics. It never returns.

func InvokeRestart Uses

func InvokeRestart(ctx context.Context, name string, args ...interface{})

InvokeRestart finds the innermost established restart with the given name and invokes it with the appropriate context (see WithRestart(s) vs. WithRecursiveRestart(s) for details). If it cannot find the named restart it will signal an error condition of type ConditionRestartNotFound.

func Signal Uses

func Signal(ctx context.Context, c Condition)

Signal signals that a condition c has occurred. An established handler may choose to act and/or handle (i.e. transfer control) in response to the condition.

func WithDebugger Uses

func WithDebugger(ctx context.Context, debugger Debugger, body Thunk)

WithDebugger establishes a debugger within the dynamic scope of its body.

func WithHandler Uses

func WithHandler(ctx context.Context, h Handler, body Thunk)

WithHandler executes its body within a dynamic context extended with a single Handler.

func WithHandlers Uses

func WithHandlers(ctx context.Context, hh []Handler, body Thunk)

WithHandlers executes its body within a dynamic context extended with an ordered set of Handlers. If a Condition is signalled during the execution of the body, then each Handler is executed in a context in which none of the given Handlers has been established (in order to avoid recursive handler errors); if all Handlers decline, then Handlers established in the enclosing dynamic context are given an opportunity to Handle the Condition.

func WithRecursiveRestart Uses

func WithRecursiveRestart(ctx context.Context, name string, restart Restart, body Thunk)

WithRecursiveRestart establishes a single restart as per WithRestarts.

func WithRecursiveRestarts Uses

func WithRecursiveRestarts(ctx context.Context, restarts map[string]Restart, body Thunk)

WithRecursiveRestarts executes its body in a dynamic context extended with a set of restarts. If invoked, a restart is invoked in the same dynamic context as its body — i.e., one in which it and its sibling restarts are established. This is rarely needed but is available for complex circumstances.

func WithRestart Uses

func WithRestart(ctx context.Context, name string, restart Restart, body Thunk)

WithRestart establishes a single restart as per WithRestarts.

func WithRestarts Uses

func WithRestarts(ctx context.Context, restarts map[string]Restart, body Thunk)

WithRestarts executes its body in a dynamic context extended with a set of restarts. If invoked, each restart is invoked in the dynamic context passed to WithRestarts — i.e., in a context in which it & its sibling restarts are not established.

type Condition Uses

type Condition interface {
    Condition() string
}

A condition represents any condition which should be signalled. It need not represent only an exceptional condition.

func NewCondition Uses

func NewCondition(s string) Condition

NewCondition returns a new condition whose message is the given string.

type ConditionRestartNotFound Uses

type ConditionRestartNotFound string

ConditionRestartNotFound indicates that a restart was not found in the current context.

func (ConditionRestartNotFound) Condition Uses

func (c ConditionRestartNotFound) Condition() string

Condition implements the Condition interface.

type Debugger Uses

type Debugger Handler

A Debugger is just a Handler which never returns. If it does, its enclosing debugger is invoked. If the outermost debugger returns, then the process panics.

type Handler Uses

type Handler func(context.Context, Condition)

A Handler is a function which may choose to handle a Condition by transferring control. It may decline to handle the Condition simply by returning.

type Restart Uses

type Restart func(context.Context, ...interface{})

A Restart is a function which is called for its side effects; after it executes control is transferred to the location where the Restart was established.

type Thunk Uses

type Thunk func(context.Context)

A thunk is a chunk of executable code. It takes only a context and returns no values; arguments & return values must be emulated with side effects.

Package conditions imports 2 packages (graph). Updated 2019-06-15. Refresh now. Tools for package owners.