-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Adding switch to disable assertions #7732
Comments
Ah, I guess I forgot |
Checked in my tentative implementation (mlhetland/julia@366e00a). |
An alternative would be to keep |
I would want an assert that check only when running in some sort of explicit debug mode. That way, asserts that are too costly for normal use can be added. |
Closing in favor of #10614 |
After a discussion on Julia-Users, it seems to me that at least some might agree with me that being able to turn off assertions could be a good idea. This is permitted in most languages, it seems. My suggestion is to follow the example of D, which I believe has made some sensible decisions on assertions and DbC in general. Their solution is to split assertions into two types, named
assert
andenforce
. They then have a command-line switch named--release
that turns offassert
but leavesenforce
in place. If we adopt such a switch, with an accessor similar toisinteractive()
, it can be used also by others, similar to (but the negation of) the__debug__
variable in Python, which is set toTrue
only if the-O
switch is not supplied.My suggestion is:
--release
at the Julia level (i.e., inclient.jl
) with a subsequently available accessorisrelease()
, which is exported, so it can also be used by others (though this is an ancillary benefit).assert
toenforce
and@assert
to@enforce
.assert
and@assert
that useisrelease()
to determine whether to become a no-op or to delegate toenforce
and@enforce
, respectively. This will not entirely eliminate the cost of calls toassert
, of course—one will still need to evaluate the arguments and execute theif
-statement, but it will eliminate the cost (and the functionality) of@assert
in release code.The idea, then, is to use
assert
and@assert
more for testing purposes, debugging and for checking for programming errors, and to useenforce
and@enforce
for error handling and input checking, and for any other kind of assertions one would want to keep in the code, regardless of release status.I have made a stab at implementing the switch, function and macro. Because the switch only takes effect once
client.jl
is loaded, I have renamed the uses of assert to enforce inbase/
(renamed all uses, for consistency, though not strictly necessary), but I left the uses intest/
alone. (I have updatedrepl.c
but not docs, manpage orhelpdb.jl
, for example. And I haven't added any tests, as I didn't find any for the original assertion functionality, and I'm not sure what infrastructure you'd want to use to test command-line switches…)The new function/macro are defined as follows—which I believe is not entirely correct…
It works for my simple ad hoc tests, but fails in, for example,
test/bitarray.jl
, where a call to@assert
occurs inside another macro with an argument also namedex
. I then get complaints onex
not existing. So I guess my quoting mojo isn't quite up to snuff, but I'm guessing this should be easy to fix? (Just calling@enforce
gives me the wrong kind of return value, as far as I can see.)Anyone interested in this? Should I submit a WIP pull request?
The text was updated successfully, but these errors were encountered: