sylvrs/libresult
最新稳定版本:0.1.1
Composer 安装命令:
composer require sylvrs/libresult
包简介
A library to add the Result monad from other popular programming languages
README 文档
README
This library is a PHP-based implementation of the Result monad that exists in many popular programming languages, including Haskell, Elm, Kotlin, and Rust.
Installation
To install this library using Composer, run the following command:
composer require sylvrs/libresult
Usage
The usage of the library follows similar standards to that of monads in other languages.
Creating a result
The basic creation of a result is as follows:
/** * @return Result<string, string> */ function create_result(): Result { return mt_rand(0, 1) > 0 ? new Ok("success!") : new Err("error!"); } $result = create_result();
Value Extraction
From there, there are several ways to get the values from the result:
// Using PHP's match expression $value = match (true) { $result instanceof Ok => $result->getValue(), $result instanceof Err => $result->getError(), // This will never occur, but is required for PHPStan to be happy in most levels default => assert(true) }; // Using `Result->get()` // Note: This will return null if the result is an error $value = $result->get(); // Using `Result->unwrap()` // Note: This will throw an exception if the result is an error $value = $result->unwrap();
Useful Methods
Furthermore, there are other methods that may prove useful for working with results:
// Using `Result->asArray()` // If the result is Ok, the array shape is [TValue, null]. Otherwise, the array shape is [null, TError] [$value, $error] = $result->asArray(); // Using `Result->getOr()` $value = $result->getOr("default value");
Error Handling
For cases where you know that the result is an error, you can use Result->unwrapError():
// Note: If the result is *not* an error, this will throw an exception $error = $result->unwrapError();
Issues / Feature Requests
Any issues or requests should be reported here.
sylvrs/libresult 适用场景与选型建议
sylvrs/libresult 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 182 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sylvrs/libresult 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sylvrs/libresult 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 182
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-16