Page MenuHomeDevCentral

Introduce Result class with Ok and Err types for status handling
ClosedPublic

Authored by dereckson on Sep 2 2018, 00:02.
Tags
None
Referenced Files
Unknown Object (File)
Wed, Dec 18, 00:23
Unknown Object (File)
Tue, Dec 17, 23:20
Unknown Object (File)
Fri, Dec 13, 14:30
Unknown Object (File)
Thu, Dec 12, 19:01
Unknown Object (File)
Mon, Dec 9, 09:40
Unknown Object (File)
Sat, Dec 7, 11:04
Unknown Object (File)
Sat, Dec 7, 03:20
Unknown Object (File)
Sat, Dec 7, 03:20
Subscribers
None

Details

Summary

This change adds a Result class to improve error and success handling.

Inspired by Rust generic type Result, this approach allows functions to return
either an Ok type for successful outcomes or an Err type for errors.

In PHP, where generics and enums are not natively available, Result serves
as a base class with two implementations:

  • Err for exceptions
  • Ok for successful values

This pattern provides a more structured and readable way to handle
results and errors explicitly compared to traditional exception handling.

Reference:
https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html

Test Plan

Implement in Zed story engine

Diff Detail

Repository
rKERUALD Keruald libraries development repository
Lint
Lint Warnings
SeverityLocationCodeMessage
Warningomnitools/src/DataTypes/Result/Err.php:24PHPCS.W.Generic.Files.LineLength.TooLongGeneric.Files.LineLength.TooLong
Unit
Test Failures
Branch
Result
Build Status
Buildable 5692
Build 5974: arc lint + arc unit

Event Timeline

dereckson created this revision.

Some ideas to make this code more useful:

  • a constructor to allow new Ok(42)
  • implement map methods:
    • or_else(defaultValue) -> return defaultValue for Err, $this->value for Ok
    • map() -> no op for Err, change value allowing type mutability for Value
    • map_err() -> no op for Value, change value allowing any Exception for Err

In PHP 8.1, could be interesting to do this:

Result sample use
use Keruald\OmniTools\DataTypes\Result;
use Keruald\OmniTools\DataTypes\ResultShape;

#[ResultShape(string, string)]
function doSomething() : Result {
    return ...;
}

$result = match (doSomething()) {
    $result->isOk() => $result->getValue(),
    default => throw new RuntimeException($result->getError()),
};

For this to work, we need two things:

  • Declare getError() in Result too
  • A ResultShape attribute to be able to typehint the Result

Moved to keruald monorepo and rebased.

dereckson retitled this revision from Implement a similar datatype than Rust result to Introduce Result class with Ok and Err types for status handling.Nov 11 2024, 13:59

Update PHPUnit tests for PHPUnit 10+

Switch from Exception to Thowrable. Implement constructors/map/mapErr/or_else per previous comment.

Switch method names to camel case

This revision is now accepted and ready to land.Nov 11 2024, 15:16
This revision was landed with ongoing or failed builds.Nov 11 2024, 15:17
This revision was automatically updated to reflect the committed changes.