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

套件詳情v6.2.0

oxidized

Rust 風格的類型 Option 和 Result 的定義,以促進更安全的程式設計。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1.1萬
喜歡數
52
Pub 分數
145

安裝套件

flutter pub add oxidized

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Oxidized 為 Dart 提供了受 Rust 啟發的 Option 與 Result 類型,實現更安全、更具表現力的錯誤處理。透過建模可選值(Option)與可能失敗的操作(Result),它幫助開發者避免與 null 相關的錯誤,並明確處理錯誤。這些類型鼓勵使用函數式程式設計模式,提升程式碼的清晰度與穩健性。該套件輕量且可無縫整合至所有主要平台上的既有 Dart 項目中。

適用情境

  • 安全處理可空值
  • 用顯式的 Option 使用代替 null 檢查
  • 使用成功/失敗狀態管理函數結果
  • 編寫韌性錯誤處理邏輯
  • 在Dart中採用函數式程式設計模式

主要特色

  • 類似 Rust 的 Option 類型
  • 類似 Rust 的 Result 類型
  • 透過顯式處理實現空值安全性
  • 函數式程式設計工具
  • 跨平台相容性

適合對象

  • 尋求 null 安全替代方案的開發者
  • 採用函數式程式設計風格的團隊
  • 需要強大錯誤管理的項目
  • 針對多個平台的 Dart 應用程式
  • 那些希望減少空引用導致的執行時崩潰的人

使用建議

  • 開發者對 Option/Result 不熟悉時的學習曲線
  • 簡單情況下的潛在冗餘
  • 需要仔細整合以避免過度設計
  • 不是 Dart 內建空安全的替代品
  • 可能會增加小型或簡單函數的複雜性

原始 README

以下為關聯原始碼儲存庫的 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.

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案