crab 1.0.0 icon indicating copy to clipboard operation
crab: ^1.0.0 copied to clipboard

Provides Rust-like Result and Option types, making it easier to handle error and option values.

🦀crab🦀 #

Provides Rust-like Result and Option types, making it easier to handle error and option values.

Defined types #

Result #

Result is the type used for returning and propagating errors.

A simple function returning Result might be defined and used like so:

enum Version { version1, version2 }

Result<Version, String> parseVersion(int versionNum) {
  if (versionNum == 1) {
    return const Ok(Version.version1);
  }
  if (versionNum == 2) {
    return const Ok(Version.version2);
  }
  return const Err('invalid version');
}

void main() {
  final version = parseVersion(1);
  // Define processing for success and error
  if (version.isErr) {
    print('error parsing header: ${version.unwrapErr()}');
  } else {
    print('working with version: ${version.unwrap()}');
  }

  // It can also be defined as follows
  parseVersion(1).mapOrElse(
    (err) => print('error parsing header: $err'),
    (v) => print('working with version: $v'),
  );
}

Option #

Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not.

Dart already supports null safety by appending a ? to a type, but you can use the Option type to enhance the handling of optional value (which can also be interpreted as nullable values).

A simple function returning Option might be defined and used like so:

Option<int> getVersionNum(List<int> header) {
  if (header.isEmpty) {
    return const None();
  }
  return Some(header.first);
}

void main() {
  final versionNum = getVersionNum([1, 2, 3, 4]);
  // Define the processing when the value can be obtained and when it cannot be
  // obtained
  if (versionNum.isNone) {
    print('invalid header length');
  } else {
    print('version number: ${versionNum.unwrap()}');
  }

  // The above process is equivalent to the following process
  getVersionNum([1, 2, 3, 4]).mapOrElse(
    () => print('invalid header length'),
    (vn) => print('version number: $vn'),
  );
}

References #

This package was created with reference to the following implementation and description of Rust.

Result #

Option #

0
likes
140
pub points
57%
popularity

Publisher

unverified uploader

Provides Rust-like Result and Option types, making it easier to handle error and option values.

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

meta, tuple

More

Packages that depend on crab