-
Notifications
You must be signed in to change notification settings - Fork 663
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
Idea: @psalm-never-throw
, alike to @psalm-pure
, but promises to not throw/warn
#2912
Comments
Good idea, the naming you proposed is more verbose but I would propose |
@letnando |
I wonder if Then presumably a It's a bit reminiscent of Java's checked exceptions, but with function by function opt-in checking. |
@bdsl doesn't |
Ah no, I didn't mean that a |
For that case would it just make sense to treat |
PHPStan uses |
One package that I think takes advantage of this is https://github.com/pepakriz/phpstan-exception-rules/. |
Didn't know about |
I would prefer |
If we wanted a solution without new syntax I'd write it as |
@M1ke I don't think I'd expect |
@bdsl Yes, this distinction is called checked/unchecked exceptions. You should declare Unchecked exceptions are for stuff that can break but shouldn't be handled in code, but instead prevented by validations, or fixed in infrastructure. This for example includes |
Doesn't |
I have a couple of thoughts, but I'm not sure of their usefulness.
This would mean that a given project's maintainer could decide to opt in to this behaviour globally, then suppress it either at the directory, file, class or function level. I do something similar for |
Not really what I had in mind, since the example above (workers, listeners, etc) are all but pure code 🤔 |
Oh sorry, I thought this was an extra protection on top of pure functions & immutable classes, not a separate (but related) track |
In that case this really is identical to /**
* @psalm-never-throws
*/
class SomeClass {
public function someMethod(Foo $foo) {
$foo->thisMethodMustHaveANeverThrowAnnotation();
echo (string) $foo; // so should this __toString()
echo $foo->aMagicGetWouldNeedItToo;
}
} I can see the actual downstream enforcement of that being a bit of a slog – you'd have to make a potentially non-trivial amount of code changes to pass these checks (the same is obviously true of pure/immutable annotations, but at least that cost/benefit ratio is widely understood by the FP community). This is not something I'm likely to want to implement in Psalm for myself, but I'll gladly accept a PR – I just created a special |
Similar to
@psalm-pure
, it may be interesting to have something like@psalm-never-throw
, in which the guarantees are:This is interesting when declaring interfaces with strong guarantees, such as:
The code in any
Worker#run()
implementations must therefore only rely on other@psalm-never-throw
implementations, or catch any exceptions declared in code that has@throws
.Triggering errors/warnings,
exit
,die()
, as well as thethrow
construct are forbidden in classes/functions annotated with@psalm-never-throw
.No further guarantees are really possible with the language, but this should play nicely with libraries such as
webmozart/assert
, orthecodingmachine/safe
.Again, feel free to shoot this down: I'm only throwing this out here as an idea 😉
A list of examples where this is useful:
[Event] -> IO [Command]
, which should not crash the callerEDIT: as highlighted by @ondrejmirtes in #2912 (comment),
@throws void
is a viable alternative syntaxThe text was updated successfully, but these errors were encountered: