FLUTTER ECOSYSTEM

nlfiedler/oxidized

包含類似 Rust 的 Option 和 Result 類型的 Dart 套件。

氧化的 專案封面
Stars
42
Forks
9
最近推送(UTC)
2024年7月23日
專案狀態
未封存
Nathan Fiedler GitHub avatar
GITHUB User

Nathan Fiedler ↗

A sometimes competent programmer.

SF Bay Area
語言Dart

此儲存庫發佈的套件

使用的依賴

依賴清單 3 項

原始 README

以下為英文專案原文快照,最新內容請造訪 GitHub。

展開 / 收合專案 README

oxidized

A Dart package with types similar to those found in Rust, such as the Result which represents either a value or an error, and Option which either contains Some value or None.

Motivation

The Option and Result types are akin to the "sum types" found in languages such as OCaml and Rust. They encourage safer code by making errors and possible "null" values (i.e. None) a necessary part of the code flow, requiring you to deal with these cases appropriately. For an illustration of how this style of coding can prove to be helpful, see the excellent tutorial series by Matt Rešetá Flutter TDD Clean Architecture Course – Entities & Use Cases in which he uses the Either type from the dartz package to represent either a success or a failure. The Result type in this package is specifically designed for this purpose, making it easy to remember which part is the "success" and which is the "failure" (imagine trying to remember "Is the right side the failure? Or was it the left?").

Usage

A simple example using synchronous I/O to avoid nested futures is shown below. For additional examples, see the Dart code in the example directory.

import 'dart:io';
import 'package:oxidized/oxidized.dart';

Result<String, Exception> readFileSync(String name) {
  return Result.of(() {
    return File(name).readAsStringSync();
  });
}

void main() {
  var result = readFileSync('README.md');

  result.match((text) {
    print('first 80 characters of file:\n');
    print(text.substring(0, 80));
  }, (err) => print(err));

  // or in a more functional way:
  final msg = result.when(
    ok: (text) => 'first 80 characters of file:\n$text.substring(0, 80)',
    err: (err) => err,
  );
  print(msg);
}

Prior Art

The dartz package offers many functional programming helpers, including the Either type, which is similar to Result, with the difference being that it represents any two types of values.

The either_option package has both Either and Option and supports all of the typical functional operations.

The result package offers a few basic operations and may be adequate for simple cases.

The rust_like_result offers a simple Result type similar to the one in Rust.

The simple_result package provides a similar Result type based on the type of the same name in Swift.