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

[5.5] Rescue helper #21010

Merged
merged 5 commits into from
Sep 6, 2017
Merged

[5.5] Rescue helper #21010

merged 5 commits into from
Sep 6, 2017

Conversation

BrandonShar
Copy link
Contributor

Optionals introduced a great helper for one of my favorite pieces of terse ruby syntax, so I thought I'd take a stab at another.

This rescue helper would allow a terser syntax for a typical try catch pattern that returns a default of some kind. It's held back a bit by PHP's anonymous function syntax, but I still think it's fairly nice.

return rescue(function () use ($someClass) {
   return $someClass->method();
}, $someClass->backupMethod());

@BrandonShar
Copy link
Contributor Author

Thanks for the feedback, but that's a ruby specific hang up that doesn't apply here. PHP's catch(\Exception $e) doesn't include things like syntax errors, it's a much less broad.

@antonkomarev
Copy link
Contributor

Since we've got PHP 7 as minimum requirement maybe we should catch \Throwable interface then?

@BrandonShar
Copy link
Contributor Author

I think that makes sense. I'm reading over examples of where that comes up now and it would be nice for things like TypeError

@BrandonShar
Copy link
Contributor Author

changed to Throwable. Good call @a-komarev

@taylorotwell taylorotwell merged commit 1101aa6 into laravel:5.5 Sep 6, 2017
@BrandonShar BrandonShar deleted the rescue-helper branch September 6, 2017 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants