Page MenuHomeDevCentral

Implement getValueOr, getValueOrElse, or, orElse for Result and Option
Changes PlannedPublic

Authored by dereckson on Wed, Nov 5, 01:38.

Details

Summary

For both Result and Option types:

  • getValueOr, GetValueOrElse returns the value or a default value
  • or, orElse allows to chain operations

To pick between the -or or -orElse variant:

  • -or is for eager evaluation or provide a scalar
  • -orElse is for lazy evaluation of a callable

The new methods follow the same patterns than or, or_else,
unwrap_or and unwrap_or_else in Rust standard library.

Ref T2173

Test Plan
  • unit tests
  • User::resolveUserID in Obsidian Workspaces (D3846)

Diff Detail

Repository
rKERUALD Keruald libraries development repository
Lint
Lint Errors
SeverityLocationCodeMessage
Erroromnitools/src/Events/Propagation.php:49PHPCS.E.Generic.Files.LineLength.MaxExceededGeneric.Files.LineLength.MaxExceeded
Erroromnitools/tests/DataTypes/Option/NoneTest.php:54PHPCS.E.Generic.Formatting.DisallowMultipleStatements.SameLineGeneric.Formatting.DisallowMultipleStatements.SameLine
Erroromnitools/tests/DataTypes/Option/SomeTest.php:63PHPCS.E.Generic.Formatting.DisallowMultipleStatements.SameLineGeneric.Formatting.DisallowMultipleStatements.SameLine
Warningomnitools/src/DataTypes/Option/Some.php:56PHPCS.W.Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassGeneric.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
Warningomnitools/src/DataTypes/Option/Some.php:60PHPCS.W.Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassGeneric.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
Warningomnitools/src/DataTypes/Option/Some.php:64PHPCS.W.Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassGeneric.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
Warningomnitools/src/DataTypes/Option/Some.php:68PHPCS.W.Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassGeneric.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
Warningomnitools/src/DataTypes/Result/Ok.php:60PHPCS.W.Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassGeneric.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
Warningomnitools/src/DataTypes/Result/Ok.php:64PHPCS.W.Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassGeneric.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
Unit
Tests Passed
Branch
result-option-or
Build Status
Buildable 6144
Build 6428: arc lint + arc unit

Event Timeline

dereckson created this revision.
  • Ready for Error
  • For Err::orElse and Err::getValueOrElse, should we pass the error to the callback?

Pass the error to the callback
Should it be a mandatory parameter of the callback?
Call to -orElse can be to return a fixed, error-independent value, but use a lazy evaluation.

For a facultative one, $argc = (new CallableElement($callable))->countArguments(); can help

Administrative block
This changes breaks compatibility, it needs coordination to ship with the other BC features (ia PHP 7 compatibility removal) to the 1.0 branch.