Skip to content

libanvl/opt

Repository files navigation

CI CodeQL Nuget (with prereleases) codecov

libanvl.Opt

A null-free optional value library for .NET with an emphasis on minimizing additional allocations.

  • An optional value is represented as the struct Opt<T>
  • A possible value or error is represented as the struct Result<T, E>
  • A zero to N values are represented as the struct Any<T>

See the Examples Tests for more on how to use Opt.

Requirements

.NET 8

Releases

  • NuGet packages are available on NuGet.org
    • Embedded debug symbols
    • Source Link enabled
  • NuGet packages from CI builds are available on the libanvl GitHub feed

libanvl.Opt Features

  • Immutable
  • Use Opt<T> instead of T? for optional values
  • Implicit conversion from T to Opt<T>
  • Deep selection of properties in complex objects
  • SomeOrDefault() for any type
  • Explicitly opt-in to exceptions with Unwrap()
  • Cast inner value to compatible type with Cast<U>()
  • Opts of IEnumerable<T> are iterable

libanvl.Result Features

  • Create success results with Result.Ok
  • Create error results with Result.Err
  • Unwrap values with Unwrap, throwing if the result is an error
  • Match on success or error with Match
  • Convert between Opt and Result

libanvl.OneOrMany Features

  • OneOrMany<T> for a single value or a collection of values
  • Implicit conversions
  • Equality operators

libanvl.Any Features

  • Any<T> for a single value or multiple values
  • Implicit conversions
  • Equality operators
  • Add and remove elements
  • Convert to array, list, or enumerable
  • Convert to Opt<T>
  • Cast elements to a compatible type

libanvl.AnyMap Features

  • Dictionary-like collection that maps keys to values of type Any<V>
  • Implements IAnyRefMap<K, V> for reference-based operations
  • Get or add value by reference with GetOrAddValueRef
  • Get value by reference with GetValueRef
  • Try to get value by reference with TryGetValueRef
  • Perform actions on each reference with ForEachRef
  • Enumerate references with GetEnumerator