パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v2.0.0

either_option

関数型プログラミングのコンセプトを使用したエラー処理のためのシンプルなライブラリ

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
570
いいね数
42
Pub ポイント
150

パッケージをインストール

flutter pub add either_option

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

関数型プログラミングにインスパイアされた軽量なDartライブラリで、堅牢なエラー処理を可能にする`Either`型を提供しています。成功した値(`Right`)またはエラー(`Left`)として結果を表現することで、安全な計算を実現します。このアプローチにより、例外の回避とコード内の明示的なエラー管理が促進されます。非同期および関数型ワークフローにおいて、よりクリーンで予測可能な制御フローを求める開発者にとって理想的です。Dartエコシステムとシームレスに統合され、すべての主要プラットフォームをサポートしています。

用途

  • 非同期操作におけるエラー処理
  • 関数型スタイルのデータ処理
  • 安全なパースと検証
  • try-catch ブロックの置き換え
  • 誤りを引き起こしやすい関数の作成
  • レジリエントなビジネスロジックの構築

主な機能

  • `Either<Left, Right>` 型を提供します
  • 関数的合成をサポート
  • 変更不可能かつ型安全
  • 最小限のボイラープレート
  • async/await と連携可能
  • クロスプラットフォーム互換性

適している対象

  • 関数型プログラミングパターンを使用する開発者
  • エラーの安全性を最優先とするチーム
  • クリーンなエラー伝播を必要とするプロジェクト
  • Dart開発者が例外を回避する
  • 再利用可能で合成可能な論理を構築している人
  • 予測可能な状態遷移を必要とするアプリケーション

注意事項

  • 関数型のコンセプトを理解する必要があります
  • 初心者にとって学習曲線が上がる可能性があります
  • 単純なエラーの場合には適していません
  • 明示的なエラー型とのみ使用可能
  • 他の FP ツールと最もよく併用される
  • 複雑なチェーンでは、慎重な型の整合が必要です

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

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 :+1: :+1:
map :+1: :+1:
flatMap :+1: :+1:
getOrElse :+1:
orElse :+1:
toLeft :+1:
toRight :+1:
toEither :+1:
Option.empty :+1:
Option.of :+1:
filter :+1:
exists :+1:
contains :+1:
swap :+1:
cond :+1: :+1:

Example

Tests

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。