-
-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat(option): add Option::proceed()
method
#433
Conversation
Pull Request Test Coverage Report for Build 7349487827
💛 - Coveralls |
Hello, Thanks for the PR. For Result, we have psl/src/Psl/Result/ResultInterface.php Lines 132 to 145 in 0aa7117
I've mentioned something similar in the original PR: https://github.com/azjezz/psl/pull/356/files#r912345716 The conclusion in there was this one: The same thing can be achieved by using $try_division = static function (int $dividend, int $divisor) use ($checked_divisor): string {
return $checked_divisor($dividend, $divisor)->mapOrElse(
static fn ($value) => sprintf('%s / %s = %s', $dividend, $divisor, $value),
static fn() => sprintf('%s / %s failed!', $dividend, $divisor),
)->unwrap();
}; From my point of view, this seems sufficient in line with your implementation as well. If you are OK with calling |
For sure this can be done in userland! I was just imagining a way to make some operations easier after learning Rust's We can close it if you already concluded that you don't want a function just for this |
I've given it some second thoughts and do agree that this shortcut is more than welcome. I've skimmed through some of my packages and I frequently see me using the combination of But to keep things consistent throughout the codebase, I suggest to rename it to The signature should be: /*
* @template Ts
*
* @param (Closure(T): Ts) $some
* @param (Closure(): Ts) $none
*
* @return Ts
*/
public function proceed(Closure $some, Closure $none): mixed; That way, proceed will be used to map any possible scenario and unwind the result in one fixed type. That way, we can keep a consistent API throughout all these monad-like classes and interfaces. The example will look like this: $try_division = static function (int $dividend, int $divisor) use ($checked_divisor): string {
return $checked_divisor($dividend, $divisor)->proceed(
some: static fn ($value) => sprintf('%s / %s = %s', $dividend, $divisor, $value),
none: static fn() => sprintf('%s / %s failed!', $dividend, $divisor),
);
}; WDYT? |
Option::match()
methodOption::proceed()
method
I think this one is ready too! 😄 |
Looks good, can you resolve conflicts so that it can be merged, |
Ready to go @veewee! |
Nice, thanks! |
[](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [azjezz/psl](https://github.com/azjezz/psl) | `2.8.0` -> `2.9.0` | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- > [!WARNING] > Some dependencies could not be looked up. Check the Dependency Dashboard for more information. --- ### Release Notes <details> <summary>azjezz/psl (azjezz/psl)</summary> ### [`v2.9.0`](https://github.com/azjezz/psl/releases/tag/2.9.0): Lenalee - 2.9.0 [Compare Source](https://github.com/azjezz/psl/compare/2.8.0...2.9.0) #### What's Changed - Apply fixes for Psalm 5.17 by [@​veewee](https://github.com/veewee) in [https://github.com/azjezz/psl/pull/431](https://github.com/azjezz/psl/pull/431) - feat(type): add class_string types ([#​432](https://github.com/azjezz/psl/issues/432)) by [@​zerkms](https://github.com/zerkms) in [https://github.com/azjezz/psl/pull/435](https://github.com/azjezz/psl/pull/435) - feat(option): add `Option::zip()`, `Option::zipWith()` and `Option::unzip()` methods by [@​devnix](https://github.com/devnix) in [https://github.com/azjezz/psl/pull/434](https://github.com/azjezz/psl/pull/434) - feat(option): add `Option::proceed()` method by [@​devnix](https://github.com/devnix) in [https://github.com/azjezz/psl/pull/433](https://github.com/azjezz/psl/pull/433) - feat(option): new `Option::apply()` method by [@​devnix](https://github.com/devnix) in [https://github.com/azjezz/psl/pull/426](https://github.com/azjezz/psl/pull/426) #### New Contributors - [@​zerkms](https://github.com/zerkms) made their first contribution in [https://github.com/azjezz/psl/pull/435](https://github.com/azjezz/psl/pull/435) **Full Changelog**: azjezz/psl@2.8.0...2.9.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/ben-challis/sql-migrations). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xMDMuMSIsInVwZGF0ZWRJblZlciI6IjM3LjEwMy4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
This is another idea of implementing a
match
method that I had after learning more bits of Rust: https://doc.rust-lang.org/rust-by-example/std/option.htmlWDYT?