Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Result\try_catch shortcut #403

Merged
merged 1 commit into from
Mar 1, 2023
Merged

Conversation

veewee
Copy link
Collaborator

@veewee veewee commented Feb 28, 2023

Inspired on ramda's tryCatch function:
https://ramdajs.com/docs/#tryCatch

tryCatch takes two functions, a tryer and a catcher. The returned function evaluates the tryer; if it does not throw, it simply returns the result. If the tryer does throw, the returned function evaluates the catcher function and returns its result.

It can be seen as a shortcut for dealing with code that you know might error, for which you want to continue the flow with e.g. a default or calculated default value.

For example:

$nullable_entity = Result\try_catch(
    static fn (): MyEntity => $repo->fetchById(1),
    static fn (): ?MyEntity => null,
);

I decided not to go the Fun way as it is in ramda, since it would require another dynamic psalm plugin.
Instead, you could make it composable yourself like this:

$tryFetchById = static fn(int $id): ?MyEntity => Result\try_catch(
    static fn (): MyEntity => $repo->fetchById($id),
    static fn (): ?MyEntity => null,
);

It also works fine with Option\from_nullable() as you can from the examples above.

❗ Note: I inlined the PromiseInterface methods into the ResultInterface and made sure the return type for them is ResultInterface. Otherwise psalm thinks ResultInterface::catch() (and others) returns a PromiseInterface instead of a ResultInterface.

@coveralls
Copy link

coveralls commented Feb 28, 2023

Pull Request Test Coverage Report for Build 4294599427

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.0002%) to 99.125%

Totals Coverage Status
Change from base Build 4015321512: 0.0002%
Covered Lines: 3967
Relevant Lines: 4002

💛 - Coveralls

@veewee veewee added Priority: Medium This issue may be useful, and needs some attention. Status: Completed Nothing further to be done with this issue. Awaiting to be closed by the requestor out of politeness Status: Review Needed The issue has a PR attached to it which needs to be reviewed. Type: Enhancement Most issues will probably ask for additions or changes. labels Feb 28, 2023
@veewee veewee requested a review from azjezz February 28, 2023 14:05
Copy link
Owner

@azjezz azjezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks awesome!

@veewee veewee added this to the 2.5.0 milestone Mar 1, 2023
@veewee veewee merged commit 37fef88 into azjezz:next Mar 1, 2023
renovate bot referenced this pull request in ben-challis/sql-migrations Mar 18, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [amphp/file](https://togithub.com/amphp/file) | require | patch |
`3.0.0-beta.6` -> `3.0.0` |
| [azjezz/psl](https://togithub.com/azjezz/psl) | require | minor |
`2.4.1` -> `2.5.0` |

---

### ⚠ Dependency Lookup Warnings ⚠

Warnings were logged while processing this repo. Please check the
Dependency Dashboard for more information.

---

### Release Notes

<details>
<summary>amphp/file</summary>

### [`v3.0.0`](https://togithub.com/amphp/file/releases/tag/v3.0.0):
3.0.0

[Compare
Source](https://togithub.com/amphp/file/compare/v3.0.0-beta.6...v3.0.0)

Stable release compatible with AMPHP v3 and fibers! 🎉

As with other libraries compatible with AMPHP v3, most cases of
parameters or returns of `Promise<ResolutionType>` have been replaced
with `ResolutionType`.

-   Renamed `BlockingDriver` to `BlockingFilesystemDriver`
-   Renamed `EioDriver` to `EioFilesystemDriver`
-   Renamed `ParallelDriver` to `ParallelFilesystemDriver`
-   Renamed `StatusCachingDriver` to `StatusCachingFilesystemDriver`
-   Renamed `UvDriver` to `UvFilesystemDriver`
-   Renamed `Amp\File\Sync\AsyncFileMutex` to `Amp\File\FileMutex`
-   Added `?Cancellation` as first parameter of `File::read()`
-   Added `File::isSeekable()`
-   Removed `File::SEEK_SET`, `File::SEEK_CUR`, and `File::SEEK_END`
-   Added `Amp\File\Whence` for seeking instead

</details>

<details>
<summary>azjezz/psl</summary>

### [`v2.5.0`](https://togithub.com/azjezz/psl/releases/tag/2.5.0):
Lenalee - 2.5.0

[Compare Source](https://togithub.com/azjezz/psl/compare/2.4.1...2.5.0)

#### What's Changed

##### Features

- Introduce Result\try_catch shortcut by
[@&#8203;veewee](https://togithub.com/veewee) in
[https://github.com/azjezz/psl/pull/403](https://togithub.com/azjezz/psl/pull/403)

##### Fixes

- Imrovements on file creation during write modes by
[@&#8203;veewee](https://togithub.com/veewee) in
[https://github.com/azjezz/psl/pull/401](https://togithub.com/azjezz/psl/pull/401)

**Full Changelog**: azjezz/psl@2.4.1...2.5.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.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config help](https://togithub.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- 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://app.renovatebot.com/dashboard#github/ben-challis/sql-migrations).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS4xMC4yIiwidXBkYXRlZEluVmVyIjoiMzUuMTAuMiJ9-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: Medium This issue may be useful, and needs some attention. Status: Completed Nothing further to be done with this issue. Awaiting to be closed by the requestor out of politeness Status: Review Needed The issue has a PR attached to it which needs to be reviewed. Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants