maybe 0.3.8

  • README.md
  • Example
  • Installing
  • Versions
  • new56

maybe

Pub

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());

example/main.dart

import 'package:maybe/maybe.dart';

class Update {
  final Maybe<String> title;
  final Maybe<String> description;
  Update({this.title, this.description});
}

void main(args) {
  // Update for the title, none for description
  var update =
      Update(title: Maybe.some("new title"), description: Maybe.nothing());

  // No update for title
  when(update.title, some: (v) {
    print("Updating title $v");
  });

  // if is also possible
  if(isNothing(update.title)) {
    print("No description");
  }

  // Fallback value
  print(some(update.description, "default description"));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  maybe: ^0.3.8

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:maybe/maybe.dart';
  
Version Uploaded Documentation Archive
0.3.8 Nov 19, 2018 Go to the documentation of maybe 0.3.8 Download maybe 0.3.8 archive
0.3.7 Nov 16, 2018 Go to the documentation of maybe 0.3.7 Download maybe 0.3.7 archive
0.3.6 Nov 16, 2018 Go to the documentation of maybe 0.3.6 Download maybe 0.3.6 archive
0.3.5 Nov 16, 2018 Go to the documentation of maybe 0.3.5 Download maybe 0.3.5 archive
0.3.4 Nov 16, 2018 Go to the documentation of maybe 0.3.4 Download maybe 0.3.4 archive
0.3.3 Nov 16, 2018 Go to the documentation of maybe 0.3.3 Download maybe 0.3.3 archive
0.3.2 Nov 16, 2018 Go to the documentation of maybe 0.3.2 Download maybe 0.3.2 archive
0.3.1 Nov 16, 2018 Go to the documentation of maybe 0.3.1 Download maybe 0.3.1 archive
0.3.0 Nov 16, 2018 Go to the documentation of maybe 0.3.0 Download maybe 0.3.0 archive
0.2.4 Nov 16, 2018 Go to the documentation of maybe 0.2.4 Download maybe 0.2.4 archive

All 18 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
50
Health:
Code health derived from static analysis. [more]
56
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
56
Learn more about scoring.

We analyzed this package on Dec 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:maybe/maybe.dart.

Health issues and suggestions

Fix lib/maybe.dart. (-25 points)

Analysis of lib/maybe.dart failed with 1 error:

line 130 col 25: The constructor 'Iterable.empty' does not have type parameters.

Fix lib/maybe_iterables.dart. (-25 points)

Analysis of lib/maybe_iterables.dart failed with 1 error:

line 11 col 23: The constructor 'Iterable.empty' does not have type parameters.

Fix lib/map.dart. (-0.50 points)

Analysis of lib/map.dart reported 1 hint:

line 138 col 3: This function has a return type of 'Maybe<V>', but doesn't end with a return statement.

Maintenance issues and suggestions

Running dartdoc failed. (-10 points)

Make sure dartdoc runs without any issues.

Maintain CHANGELOG.md. (-20 points)

Changelog entries help clients to follow the progress in your code.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test any