搜尋套件、儲存庫與 APP 分類

GitHub 儲存庫nlfiedler/oxidized

氧化的

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

氧化的 專案封面
星標
42
分支
9
最近推送(UTC)
2024年7月23日
專案狀態
未封存

原始 README

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

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.

Nathan Fiedler GitHub avatar
GitHub 作者

Nathan Fiedler ↗

A sometimes competent programmer.

SF Bay Area
語言Dart

此儲存庫發佈的套件

使用的依賴

依賴清單 3 項

如何選擇適合專案的儲存庫?

先依使用情境篩選,再比較專案活躍情況,最後到原始儲存庫確認使用要求。

  1. 透過儲存庫名稱、作者或描述搜尋,再依技術分類縮小範圍。
  2. 綜合星標、分支與最近推送比較專案;星標反映社群關注,不保證品質。
  3. 使用前在 GitHub 確認 README、授權條款與 Issues。

來源與維護

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗