Dart-Oxide is a set of types, extensions, and other utilities for making Dart a little Rustier. Inspiration is taken from C# and other garbage collected languages as well, as an acknowledgement that Dart is garbage collected, and certain patterns and practices aren't possible/practical in a garbage collected environment.
This package provides some of the same types as fpdart, with similar motivations behind them, however Dart-Oxide is a less drastic paradigm shift into full functional programming. Instead, Dart-Oxide seeks to apply certain principles inspired by functional programming, while still intending for users to otherwise utilize standard Dart idioms and practices.
Option<T>
, an alternative toT?
, which provides a variety of methods and extensions to provide additional functionality. UnlikeT?
, it is composable, i.e.Option<Option<T>>
is a valid type. It provides aSome(T)
variant to represent a value, and aNone
variant if no value is present.Result<R, E>
type, to make error handling more explicit. It prevents scenarios where bugs can be introduced if athrow
is introduced in a called function, but the calling code is not updated totry/catch
the new exception. It provides anOk(R)
variant if successful, and anErr(E)
variant if there was an error.Newtype
, a basic wrapper around any type that is guaranteed not to unify with the original type. Only useful for simple types (like Ids), since no methods or operators can be forwarded to the new type. Will likely be replaced by extension types when the feature is complete.
Extensions on Iterable
s and Stream
s of Option
and Result
are provided, to make working with them more ergonomic.
- A set of interfaces for disposable objects (
IDisposable
andIFutureDisposable<U>
).IFutureDisposable<U>
is generic over the return type to allow it toU
to represent aFuture
,FutureOr
, or to unify with the synchronousIDisposable
. - Static
using
andusingAsync
functions to perform an action on a disposable resource and guarantee the resource is disposed afterwards (as in C#) - a
Box<T, U>
type that can store a resource and provide protected access based on whether the resource has been disposed or not.Box
is generic over whether or not the dispose method is async. Additionally, aBox
can be attached to aFinalizer
so that the resource is disposed when theBox
is garbage collected. - an
Rc<T, U>
type, similar toBox
, except that anRc
can be cloned, and the protected resource will only be disposed when the lastRc
is disposed. Useful for managing the lifetime of shared resources that require proper disposal. LikeBox
,Rc
is generic over whether or not the dispose method is async, and can be attached to aFinalizer
so that the resource is disposed when theRc
is garbage collected. - a
Ptr<T>
type, used to pass a reference to a mutatable (usually primitive) value.
Refer to the class and method documentation, or the tests, for basic usage.
Currently, this project is structured as a monorepo, with the intention of adding more features via lints and possibly code generation using other packages. However, as I do not currently write use Dart/Flutter for any projects, these additional packages may or may not actually be created.