maybe_just_nothing 0.1.9

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

Maybe Just Nothing #

Yet another variation of the good old Maybe monad with eager execution written in Dart. Or maybe it's just nothing.

import 'package:maybe_just_nothing/maybe_just_nothing.dart';

Maybe<int> addTwoIntegers(dynamic a, dynamic b) =>
    Maybe(a).cast<int>().merge(Maybe(b).cast<int>(), (a, b) => a + b);

void main() {
  // Prints 42
  addTwoIntegers(40, 2).ifPresent(print);

  // Does nothing since one of the values is null.
  addTwoIntegers(40, null).ifPresent(print);

  // Does nothing since one of the values is not an integer.
  addTwoIntegers('Oops', 2).ifPresent(print);
}

Classes: #

  • Maybe(nullableValue) wraps a possibly null value
  • Just(42) wraps a non null value (throws ArgumentError on null)
  • Nothing<T>() represents a missing value of type T

Methods #

map #

Maps the value.

flatMap #

Maps the value.

filter or where #

Filters the value by the given predicate.

or #

Returns the wrapped value (if present), or the default value (otherwise).

orAsync #

Returns a Future of the wrapped value (if present), or the default value (otherwise).

orGet #

Returns the wrapped value (if present), or the result of the producer function (otherwise).

orGetAsync #

Returns a Future of the wrapped value (if present), or the result of the producer function (otherwise).

orThrow #

Returns the wrapped value (if present), or throws the result of the producer function (otherwise).

ifPresent #

Calls the consumer function if the wrapped value is present.

ifNothing #

Calls the callback function if the wrapped value is not present.

cast #

Narrows the type to P if the value is present and has actually the type of P.

merge #

Merges the other Maybe using the merger function.

chain #

A sort of "Chain of Responsibility" pattern. Returns the next Maybe if this is empty.

Q&A #

Where is isPresent?

It is not there by design. Most of the times using ifPresent() and ifNothing() should be sufficient. If there is no choice but inline logic, use the type checks:

void fun(Maybe<int> score) {
  if (score is Just<int>) {
    doSomething(score.value); // Bonus! The score.value is available and guaranteed to be non-null.
  } else {
    // do something else
  }
}

Changelog #

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased #

0.1.9 - 2020-06-27 #

Changed #

  • ifPresent() and ifNothing() return Maybe

0.1.8 - 2020-06-13 #

Added #

  • const constructor to Nothing

0.1.7 - 2020-06-13 #

Changed #

  • Relaxed cast() type constraint

0.1.6 - 2020-06-13 #

Added #

  • chain() method

0.1.5 - 2020-06-13 #

Fixed #

  • "Future was not exported" in tests

0.1.4 - 2020-06-13 #

Changed #

  • Just and Nothing return more narrow types in certain cases

0.1.3 - 2020-06-13 #

Fixed #

  • Future was not exported

0.1.2 - 2020-06-13 #

Changed #

  • relaxed min Dart version to 2.0.0
  • minor readme fixes

0.1.1 - 2020-06-13 #

Added #

  • where is an alias for filter

0.1.0 - 2020-05-24 #

Added #

  • New methods: cast(), orAsync(), orGetAsync(), flatMap(), merge()

0.0.1 - 2020-05-23 #

Added #

  • Initial version.

example/example.dart

import 'package:maybe_just_nothing/maybe_just_nothing.dart';

Maybe<int> addTwoIntegers(dynamic a, dynamic b) =>
    Maybe(a).cast<int>().merge(Maybe(b).cast<int>(), (a, b) => a + b);

void main() {
  // Prints 42
  addTwoIntegers(40, 2).ifPresent(print);

  // Does nothing since one of the values is null.
  addTwoIntegers(40, null).ifPresent(print);

  // Does nothing since one of the values is not an integer.
  addTwoIntegers('Oops', 2).ifPresent(print);
}

Use this package as a library

1. Depend on it

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


dependencies:
  maybe_just_nothing: ^0.1.9

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:maybe_just_nothing/maybe_just_nothing.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
41
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
70
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
pedantic ^1.9.0
test ^1.9.2