maybe

Last updated:

0 purchases

maybe Image
maybe Images
Add to Cart

Description:

maybe

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();
copied to clipboard
Maybe.some : creating an optional item with a value #
final maybe = Maybe.some("hello world");
copied to clipboard
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);
copied to clipboard
some : extracting some value #
final maybe = Maybe.some("hello world");
final value = some(maybe, "default"); // == "hello world"
copied to clipboard
final maybe = Maybe<String>.nothing();
final value = some(maybe, "default"); // == "default"
copied to clipboard
isNothing : testing if some value #
final maybe = Maybe.some("hello world");
final value = isNothing(maybe); // false
copied to clipboard
final maybe = Maybe<String>.nothing();
final value = isNothing(maybe); // true
copied to clipboard
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");
copied to clipboard
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
copied to clipboard
var maybe = Maybe<String>.nothing();
var converted = mapSome<String,int>(maybe, (v) => v.length);
var value = some(converted, 0); // == 0
copied to clipboard
MaybeMap<K,V> : a map with optional values (aka Map<K, Maybe) #
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()));
copied to clipboard
Map<String,String> maybeMap = {
"test": "value",
};
var maybeMap = MaybeMap<String,String>.fromMap(maybeMap);
when(map["test"], some: (v) => print(v));
copied to clipboard
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');
});
copied to clipboard
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');
});
copied to clipboard
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);
copied to clipboard
This is really different than having a nothing title, which significates that the title shouldn't be modified.
final update = Update(title: Maybe.nothing());
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.