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
F12435726: D3847.id9963.diff
Thu, Nov 6, 02:31
F12435596: D3847.id9962.diff
Thu, Nov 6, 02:11
F12435503: D3847.id9961.diff
Thu, Nov 6, 01:51
F12434734: D3847.diff
Wed, Nov 5, 23:47
F12433446: D3847.diff
Wed, Nov 5, 20:55
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.