either_option 1.0.3

either_option #

either_option is a simple library typed for easy and safe error handling with functional programming style in Dart. It aims to allow flutter/dart developpers to use the 2 most popular patterns and abstractions : Either and Option, mainly used in FP language like Scala, Haskell, OCaml,...

Installation #

Package link on pub either_option In your pubspec.yaml dependencies add

    either_option: ^1.0.3

Overview #

Either Represents a value of one of two possible types. By convention we put missing or error value in an instance of Left and expected success value in an instance of Right.

For example, we fetch an url from a repository to get an User details and use Either<ServerError,User> :

  Future<Either<ServerError, User>> getUser(int id) async {
    final baseUrl = "https://fakerestapi.azurewebsites.net/api/Users/$id";

    final res = await http.get(baseUrl);

    if (res.statusCode == 200) {
      dynamic body = json.decode(res.body);
      User user = User.fromJson(body);
      return Right(user);
    }
    if (res.statusCode == 404)
      return Left(ServerError("This user doesn't exist"));

    return Left(ServerError("Unknown server error"));
  }

So now to consume result we can use for example fold method and say what to do with value :

main() async {
  final Repository repository = Repository();

  final Either<ServerError, User> res = await repository.getUser(3);
  final defaultUser = User(id: 0, username: "ko", password: "ko");
  final userName = res.fold((_) => defaultUser.username, (user) => user.id);
  print(userName); // "User 3"
  
//if res was a Left, print(userName) would give "ko"
}

Option Represents a value of one of two possible types. By convention we consider missing value as an instance of None and expected success value in an instance of Some.

Features #

Functions available :

OptionEither
fold👍👍
map👍👍
flatMap👍👍
getOrElse👍
orElse👍
toLeft👍
toRight👍
toEither👍
Option.empty👍
Option.of👍
filter👍
swap👍
cond:1:👍

Example #

Tests #

Changelog #

[1.0.3] - 2019-07-11

Added #

  • cond function in Either
  • cond function in Option
  • filter function in Option
  • exists function in Option

[1.0.2] - 2019-07-05

Changed #

  • minors changes : update readme

[1.0.1] - 2019-07-03

Changed #

  • update readme
  • make test version compatible with any flutter_test version

Removed #

  • all examples commented

[1.0.0] - 2019-07-03

Changed #

  • fix version typo
  • using /// for doc comments

Added #

  • seperated pubspec.yaml for example

[0.0.1] - 2019-07-03

Added #

  • Option Monad implementation
  • Either Monad implementation
  • Units tests
  • Some examples

example/either_option_example.dart

import 'package:either_option/src/either.dart';
import 'package:either_option/src/option.dart';

import 'model/server_error.dart';
import 'model/user.dart';
import 'repository_example.dart';

/// You can combine all functions defined in src/either.dart and src/option.dart
main() async {
  final Repository repository = Repository();

  print("------Either Example---------");
  final Either<ServerError, User> res = await repository.getUser(3);
  final defaultUser = User(id: 0, username: "ko", password: "ko");
  final userName = res.fold((_) => defaultUser.username, (user) => user.id);
  print(userName); // "User 3"

  print("---------Option Example---------");
  final maybeUser = await repository.getUserOpt(345);
  final userNone = maybeUser.getOrElse(defaultUser);
  print(userNone); // User(0, ko, ko)

  print("---------Option Example 2---------");
  String f(A a) => a.toString();
  final some = Option.of(B());
  print(some.map((s) => f(s)));
}

class A {
  @override
  String toString() => "Im a A";
}

class B extends A {
  @override
  String toString() => "Im a B";
}

Use this package as a library

1. Depend on it

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


dependencies:
  either_option: ^1.0.3

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:either_option/either_option.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
48
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]
74
Learn more about scoring.

We analyzed this package on Aug 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
test ^1.6.0 1.6.8
Transitive dependencies
analyzer 0.38.1
args 1.5.2
async 2.3.0
boolean_selector 1.0.5
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.2
csslib 0.16.1
front_end 0.1.23
glob 1.1.7
html 0.14.0+2
http_multi_server 2.1.0
http_parser 3.1.3
io 0.3.3
js 0.6.1+1
kernel 0.3.23
matcher 0.12.5
meta 1.1.7
mime 0.9.6+3
multi_server_socket 1.0.2
node_preamble 1.4.6
package_config 1.1.0
package_resolver 1.0.10
path 1.6.4
pedantic 1.8.0+1
pool 1.4.0
pub_semver 1.4.2
shelf 0.7.5
shelf_packages_handler 1.0.4
shelf_static 0.2.8
shelf_web_socket 0.2.3
source_map_stack_trace 1.1.5
source_maps 0.10.8
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
test_api 0.2.7
test_core 0.2.9
typed_data 1.1.6
vm_service 1.1.1
watcher 0.9.7+12
web_socket_channel 1.0.15
yaml 2.1.16
Dev dependencies
flutter_test
http ^0.12.0+2 0.12.0+2