Code Monkey home page Code Monkey logo

either_option's Introduction

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

Prerelease versions that support null safety

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

   either_option: ^2.0.0

Overview

Either

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 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.username);
  print(userName); // "User 3"
  
//if res was a Left, print(userName) would give "ko"
}

Option

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.

  Future<Option<User>> getUserOpt(int id) async {
    final res = await http.get(baseUrl);
    return Option.cond(
        res.statusCode == 200, User.fromJson(json.decode(res.body)));
  }
  // -----------------------------------------
  main() async {
  final Repository repository = Repository();

  final Option<User> res = await repository.getUserOpt(3);
  final defaultUser = User(id: 0, username: "ko", password: "ko");
  final userName = res.getOrElse(defaultUser).username;
  print(userName); // "User 3"

  //if res was a None, print(userName) would give "ko"
}

Ressources & explanation

Medium article

Features

Functions available :

Option Either
fold ๐Ÿ‘ ๐Ÿ‘
map ๐Ÿ‘ ๐Ÿ‘
flatMap ๐Ÿ‘ ๐Ÿ‘
getOrElse ๐Ÿ‘
orElse ๐Ÿ‘
toLeft ๐Ÿ‘
toRight ๐Ÿ‘
toEither ๐Ÿ‘
Option.empty ๐Ÿ‘
Option.of ๐Ÿ‘
filter ๐Ÿ‘
exists ๐Ÿ‘
contains ๐Ÿ‘
swap ๐Ÿ‘
cond ๐Ÿ‘ ๐Ÿ‘

Example

Tests

either_option's People

Contributors

cbyad avatar jeiea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

either_option's Issues

Addition of getOrNull

One of the breaking changes of adopting null safety is the following.

final n = Some(3);
// pass
n.fold(() => null, (x) => x);
// can't pass
n.getOrElse(null);

Current getOrElse implementation is this.

A getOrElse(A caseElse) => fold(() => caseElse, (A a) => a);

I couldn't find the signature accepting n.getOrElse(null). It can be bypassed by using fold directly so it doesn't require an immediate action, but it can be a consideration point.

NNBD support

It seems the package does not support NNBD and has no branch for it. Can I make a PR?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.