o/result

从 Rust 迁移的 Result 类型的基本版本

dev-master 2020-01-29 04:33 UTC

This package is auto-updated.

Last update: 2024-09-29 05:35:55 UTC


README

这是从 Rust 迁移到 PHP 的 Result 类型的部分迁移。大部分功能都已实现,但我跳过了迁移一些在 PHP 环境中不太适用的 Rust 特定方法。

使用方法

基本使用方法与 Rust 相同。

use O\Result\{ResultInterface, Ok, Err};

// basic setting and getting of values
$greeting = new Ok('hey there');
$name = new Err('');

echo $greeting->unwrap(); // echos 'hey there'
echo $name->unwrap(); // throws a ResultException
echo $name->unwrapOr('unknown'); // echos 'unknown'

// function that returns a Result<number, string>
function divide(int $x, int $y): ResultInterface
{
  if ($y === 0) {
    return new Err('cannot divide by zero');
  }
  return new Ok(x / y);
}

divide(1, 0); // Err('cannot divide by zero')
divide(1, 1); // Ok(1)

代码检查

$ composer lint

分析

$ composer analyse

测试

$ composer test:unit