-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Proposal: debug only code #1394
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
Comments
all of this is effectively accomplishable in status quo, albeit not as conveniently as you're promising. const builtin = @import("builtin");
const dbg = builtin.mode == builtin.Mode.Debug;
var x : if (dbg) bool else void = if (dbg) false; // implicit `else {}` then all usage of the variable would need to be guarded by
the above trick with
declare the parameters to become
simply
aside from the obvious
Why not use
The code I've proposed in this comment is as clunky as it is to encourage people not to do it. it's also more confusing for code to behave differently in different build modes, and using an explicit |
This is the problem I wanted to fix. It makes the code harder to read, full of trivial ifs, obfuscating the flow when the debug purpose is already clear. The gist of my proposal was that Zig could make better than the (not very good) status-quo in C.
Systematic defensive coding is much more than few asserts. It means a lot of supporting code (outside dedicated tests), code that must not make it into the final executable.
The defensive code is unavoidable (well, not really, but I wouldn't go that way). There's opportunity to make the situation better, on a very large scale. This could be one selling point for industrial use of the language, for industries obsessed with reliability. |
TL;DR: I propose
dbg
annotation to mark debug-only code. In non-debug mode all such code disappears.willl in non-debug mode turn into:
The Problem
Heavily defensive code can get quickly hard to read. In C it looks like:
In Zig one can probably accomplish similar feat with
@import("builtin").Mode == Debug
.In C such verbosity is partially fixable by macro:
Zig, AFAIK, doesn't offer such tricks.
The Solution
The compiler could support writing debug-only code with minimal ceremony. One keyword would be needed:
dbg
(ordebug
or so).By saying debug mode I mean "when and only when asserts are enabled", not the misnamed compilation regime. Release with enabled asserts is debug for me.
Local variable could be prefixed with
dbg
. It then exists only in debug mode.dbg var x : bool = false;
Any assignment to such variable would disappear outside the debug mode,. No annotation would be required.
x = true; // compiler turns it into empty line in non-debug mode
Using debug variable in statements would follow the same rule:
Assigning debug value to non-debug is not clear:
my_value = x; // error, what does this mean in non-debug mode?
Statement needs to be explicitly annotated:
dbg my_value = x;
Passing debug value to a function would be covered later.
dbg
could be used to label global variables and struct members. The same rules would apply.Passing debug parameter to function.
In C I often do:
In Zig it should be possible to mark function parameter(s) as
dbg
too.fn foo(x : bool, dbg y : int32, dbg z : bool) { ... }
and use them without any ceremony:
foo(true, 100, false);
In non-debug mode compiler turns it into:
foo(true);
and the compiler is then free to remove all code that produces these
dbg
parameters and does nothing else:would turn into:
Inside the function,
dbg
parameters would behave likedbg
local variables.If advanced
assert
is implemented (Allow assert() with custom error message #1303), then all its parameters would bedbg
.dbg
functions.Whole function could be annotated as
dbg
. Then all its parameters aredbg
, as well as its return value (no need for explicitdbg
). In non-debug such function wouldn't exist and all its invocations will disappear.To make things simpler,
dbg
function should not be allowed to return any error. Pointer to such function should go only into adbg
value.Whole code blocks could be annotated as debug:
There could be
else
block:These blocks should not create a new scope. Perhaps different syntax could be used:
Where does this help?
#ifdef
s).#ifdef
s everywhere.)The feature should not be generalized (e.g to support different API per customer).
The text was updated successfully, but these errors were encountered: