func_type_result 0.0.1 copy "func_type_result: ^0.0.1" to clipboard
func_type_result: ^0.0.1 copied to clipboard

Simple functional result-type for alternative exception-handling.

example/func_type_result_example.dart

import 'dart:math';

import 'package:func_type_result/func_type_result.dart';

final rand = Random();

T mightFail<T>(T value) {
  if (rand.nextBool()) {
    return value;
  } else {
    throw Exception("Woopsi!");
  }
}

void main() async {
  // Create a Result
  final Result<int> ok = Ok(1);
  final Result<int> err = Err(Exception("Woopsi"));
  final res = result(() => mightFail("Some value"));
  final asyncRes = asyncResult(() async => mightFail("Some value"));

  // Do stuff with it
  final Result<String> mapped = ok.map((value) => value.toString());
  final Result<Result<int>> nested = Ok(ok);
  final Result<String> flatMapped = nested.flatMap((value) => value.toString());
  final Result<String> asyncFlatMapped =
      await nested.asyncFlatMap((value) async => value.toString());

  // TODO: Add other examples
}
1
likes
150
points
28
downloads

Publisher

unverified uploader

Weekly Downloads

Simple functional result-type for alternative exception-handling.

Repository (GitLab)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

More

Packages that depend on func_type_result