dart_maybe
dart_maybe copied to clipboard
No more null check with an dart equivalent of Maybe (Haskel, Elm) / Option (F#).
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 some
or when
to access your potential value so you are forced to check its status before using it.
Maybe<T>.nothing
: creating an optional item that is empty
final maybe = Maybe<String>.nothing();
Maybe.some
: creating an optional item with a value
final maybe = Maybe.some("hello world");
final isNothing = Maybe<String>.some(null); // By default `some` with a null value is converted to `nothing`
final isNotNothing = Maybe<String>.some(null, nullable: true);
some
: extracting some value
final maybe = Maybe.some("hello world");
final value = some(maybe, "default"); // == "hello world"
final maybe = Maybe<String>.nothing();
final value = some(maybe, "default"); // == "default"
isNothing
: testing if some value
final maybe = Maybe.some("hello world");
final value = isNothing(maybe); // false
final maybe = Maybe<String>.nothing();
final value = isNothing(maybe); // true
when
: triggering an action
var maybe = Maybe.some("hello world");
when(maybe, some: (v) {
print(v); // "hello world"
});
// Defining nothing
maybe = Maybe.nothing();
when(maybe, some: (v) {
print(v); // not called!
});
// You can add a default value when nothing
maybe = Maybe<String>.some(null);
when(maybe, some: (v) {
print(v); // "hello world"
},
defaultValue: () => "hello world");
mapSome
: converts a value type to another
var maybe = Maybe.some("hello world");
var converted = mapSome<String,int>(maybe, (v) => v.length);
var value = some(converted,0); // == 11
var maybe = Maybe<String>.nothing();
var converted = mapSome<String,int>(maybe, (v) => v.length);
var value = some(converted, 0); // == 0
MaybeMap<K,V>
: a map with optional values (aka Map<K, Maybe<V>>)
var map = MaybeMap<String,String>();
map["test"] = Maybe.nothing(); // doesn't add value
map["test"] = Maybe.some("value"); // adds value
when(map["test"], some: (v) => print(v));
map["test"] = Maybe.nothing(); // deletes key
when(map["test"], isNothing: (v) => print("deleted :" + map.containsKey("test").toString()));
Map<String,String> maybeMap = {
"test": "value",
};
var maybeMap = MaybeMap<String,String>.fromMap(maybeMap);
when(map["test"], some: (v) => print(v));
What about quiver's Optional
?
The Optional type has several similarities with Maybe
, but there are several subtle differences.
Optional can be null
Let's take a quick example :
class Update {
final Optional<String> title;
final Optional<String> description;
Update({Optional<String> title, Optional<String> description})
: this.title = title ?? Optional<String>.absent(),
this.description = description ?? Optional<String>.absent();
}
final update = Update(title: Optional.of('sample'));
update.title.ifPresent((v) {
print('title: $v');
});
update.description.ifPresent((v) {
print('description: $v');
});
Thanks to static functions, all can be replaced by :
class Update {
final Maybe<String> title;
final Maybe<String> description;
Update({this.title this.description});
}
final update = Update(title: Maybe.some('sample'));
when(update.title, some: (v) {
print('title: $v');
});
when(update.description, some: (v) {
print('description: $v');
});
So, the critical part is that you can forget that Optional
can be null
itself and produce exceptions (update.title.ifPresent
in our example). You are then forced to test its nullity and you come back to the initial problematic. This is where Maybe
feels more robust to me.
absent
is similar to null
With Maybe
, values can be nullable.
In the following example, we explicitly say that the title should have a new null
value.
class Update {
final Maybe<String> title;
final Maybe<String> description;
Update({ this.title, this.description});
}
final update = Update(title: Maybe.some(null, nullable: true);
This is really different than having a nothing
title, which significates that the title shouldn't be modified.
final update = Update(title: Maybe.nothing());