maybe 0.1.2 maybe: ^0.1.2 copied to clipboard
No more null errors.
maybe #
No more null check with an dart equivalent of Maybe (Haskel, Elm) / Option (F#).
Usage #
The key is that you need to call the Maybe.when<T>
method to access your potential value so you are forced to check its status before using it.
// Defining a value
var maybe = Maybe.some("hello world");
Maybe.when(maybe, some: (v) {
print(v); // "hello world"
});
// Defining nothing
maybe = Maybe.nothing();
Maybe.when(maybe, some: (v) {
print(v); // not called!
});
// By default, null as value is considered as nothing
maybe = Maybe<String>.some(null);
Maybe.when(maybe, some: (v) {
print(v); // not called!
});
// ... but you can explictly activate null values
maybe = Maybe<String>.some(null, nullable: true);
Maybe.when(maybe, some: (v) {
print(v); // called with null
});
// You can add a default value when nothing
maybe = Maybe<String>.some(null);
Maybe.when(maybe, some: (v) {
print(v); // "hello world"
},
defaultValue: () => "hello world");