Skip to content

Simple utility types and functions for working with futures in Go.

License

Notifications You must be signed in to change notification settings

cpacifying/future

Repository files navigation

Simple utility types and functions for working with futures.

Proposed implementation is mostly trivial recombinations of standard features of Go channels and can be rightfully considered syntactic sugar and/or an indication of the author having too much free time on her hands.

However, it can save some lines in some implementation scenarios because:

  • All exported methods and functions are safe for concurrent use (assigning nil to a channel variable is not).

  • Mutating methods Success and Fail are idempotent (closing a channel is not) and never panic (sending or closing a channel sometimes do).

  • Grouping funcs (All, Any, etc) can fit into one line (select on multiple channels cannot).

Basic points and invariants

  • A future is an entity that is expected to produce a value.

  • Type parameter PT is called the produced type, and is the type of a value future is expected to produce.

  • A future can be awaited on.

  • A future is either incomplete or complete.

  • An incomplete future can complete in two ways: either succeed and produce a value, or fail with a non-nil error. This is why a completed future has two completion values: produced value and error value.

  • If a future's error completion value is not nil, produced completion value is always zero.

  • Once completed, a future never changes its state (i.e. completion status and completion values) again.

Guarantees

  • No panics. All exported methods are panic-free.

  • No data races. All exported types and methods are safe for concurrent use.

  • Incomplete futures are ok. Unreachable futures that never complete are eventually garbage collected and all related resources freed. The package user DOES NOT have to make sure created futures are eventually completed.

  • No external dependencies. All dependencies are on Go standard library except for go-cmp that is only used in tests.

About

Simple utility types and functions for working with futures in Go.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages