some 2.2.0 copy "some: ^2.2.0" to clipboard
some: ^2.2.0 copied to clipboard

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

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
copied to clipboard

Create #

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

Switch (dart > 3.0.0) #

Maybe<int> some = Some(1);

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

Check has value #

some.isSome
none.isNone
some.has
copied to clipboard

Get value #

if(some.has) {
    some.val!
}
copied to clipboard

Monadic #

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

Logical #

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

Flatten #

someSome.flatten();
copied to clipboard

Pattern matching #

some.when(some: (v) => 'some', none: () => 'none');
copied to clipboard
some.some((v) => 'some');
copied to clipboard
none.none(() => 'none');
copied to clipboard
none.defaultVal('none');
copied to clipboard
none.defaultValFn(() => 'none');
copied to clipboard
3
likes
150
points
231
downloads

Publisher

unverified uploader

Weekly Downloads

2024.09.10 - 2025.03.25

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on some