Skip to content

Some / Maybe / Option / Result / Nullable, like Haskel Maybe, Can avoid semantic confusion caused by returning null when returning a value

License

Notifications You must be signed in to change notification settings

MeowType/MaybeDart

Repository files navigation

Some / Maybe / Option / Result / Nullable

Can avoid semantic confusion caused by returning null when returning a value

like Haskel Maybe


pub MIT


Usage

Import

import 'package:some/index.dart'; // import all

Create

Maybe<int> some = Some(1);
Maybe<int> none = None();

Switch (dart > 3.0.0)

Maybe<int> some = Some(1);

var a = switch (some) {
    Some(v: var v) => v,
    null => 0
};

Check has value

some.isSome
none.isNone
some.has

Get value

if(some.has) {
    some.val!
}

Monadic

some.map((v) => 'some');
some.then((v) => 'some');
some.flatMap((v) => Some('some'));
some.andThen((v) => Some('some'));

Logical

some.and(Some('some'));
some.andThen((v) => Some('some'));
some.or(Some('some'));
some.orElse(() => Some('some'));
some.xor(Some('some'));

Flatten

someSome.flatten();

Pattern matching

some.when(some: (v) => 'some', none: () => 'none');
some.some((v) => 'some');
none.none(() => 'none');
none.defaultVal('none');
none.defaultValFn(() => 'none');

About

Some / Maybe / Option / Result / Nullable, like Haskel Maybe, Can avoid semantic confusion caused by returning null when returning a value

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages