-
-
Notifications
You must be signed in to change notification settings - Fork 13.7k
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
throw vs assert in packages #154292
Comments
The main reason I prefer
But the author also suffers:
And some more reasons:
One area where assertions are actually nice, is in simple checks, such as My preferred direction for this issue, is to make |
Note that nothing prevents us from having asserts and nice error messages:
I've been putting off converting The trick that makes |
Oh, using |
Also refer to this nix issue: NixOS/nix#749 |
So that would be something like cond: msg: x: assert cond || throw msg; x or cond: msg: assert cond || throw msg; x: x These are subtly different and I believe that the latter is better because it the prior will not fire until the last application, which may not occur. The latter is behaviorally indistinguishable from Again, the I guess you could currently use its syntax at the call sites instead, you please, but it is semantically questionable and, like any hack, may complicate the evolution of the evaluator if, assuming # I wouldn't recommend this, but I think this is what you're proposing:
assert throwIfNot foo "my nice error message";
# … I wouldn't rename |
As said, this has been my plan for quite some while.
They are different things as well and changing
How so? The assert would be evaluated before the first application, if I understanding it correctly. In any case I think this is a very slim benefit and I'm still a bit concerned creating such a foreign “DSL” type thing (see #154292 (comment)). |
|
@roberth I feel like we are moving in two different directions here, and we should standardize on one of them.
nixpkgs/pkgs/tools/networking/connman/connman.nix
Lines 52 to 53 in 92b09d4
^ this was the way most packages wrote assertions so far, and
lib.asserts
was just a slight improvement overassert
.The new approach is different and diverges from the previous best practice.
Originally posted by @Profpatsch in #153740 (comment)
cc @roberth
The text was updated successfully, but these errors were encountered: