assert and enforce both compiled out with -release

rjframe dlang at ryanjframe.com
Sat Jan 27 14:15:26 UTC 2018


On Sat, 27 Jan 2018 13:52:47 +0000, kdevel wrote:

> https://dlang.org/phobos/std_exception.html#enforce states:
> 
> | Also, do not use enforce inside of contracts (i.e. inside of in and
> out blocks | and invariants), because they will be compiled out when
> compiling with -release.
> | Use assert in contracts.
> 
> But assert is also ignored in release mode:
> 

enforce is a wrapper around try/catch (I don't know if that's technically 
true, but it is pragmatically true); assert is a debug tool. Contracts and 
assertions are removed for release builds, which means if you place an 
enforce in a contract, it's removed - but the same enforce placed in the 
function body would remain. You don't want the enforce stripped from the 
exe (if you are OK with that, it should be an assertion), so it shouldn't 
be in the contract.

Use assertions to catch programming bugs, and enforce or try/catch for 
exceptions.

If DIP 1006[0] is accepted, we'll have greater control over the removal of 
asserts and contracts in release mode.


[0]: https://github.com/dlang/DIPs/blob/master/DIPs/DIP1006.md


More information about the Digitalmars-d-learn mailing list