Page MenuHomeDevCentral

Implement getValueOr, getValueOrElse, or, orElse for Result and Option
ClosedPublic

Authored by dereckson on Wed, Nov 5, 01:38.
Tags
None
Referenced Files
F12476642: D3847.id9964.diff
Fri, Nov 7, 18:12
F12468913: D3847.id.diff
Fri, Nov 7, 12:34
F12468780: D3847.id9962.diff
Fri, Nov 7, 12:23
F12467299: D3847.id9963.diff
Fri, Nov 7, 10:59
F12465391: D3847.diff
Fri, Nov 7, 09:10
F12461660: D3847.id9963.diff
Fri, Nov 7, 06:12
F12460270: D3847.id9963.diff
Fri, Nov 7, 05:14
F12460260: D3847.id9962.diff
Fri, Nov 7, 05:14
Subscribers
None

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.

Implement or/orElse for Result. Pass error as optional argument to callback.

Let's merge this in develop/omnitools-1.x branch.

This revision is now accepted and ready to land.Wed, Nov 5, 22:06
This revision was landed with ongoing or failed builds.Wed, Nov 5, 22:07
This revision was automatically updated to reflect the committed changes.