www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - assert vs enforce.

reply "TheFlyingFiddle" <kurtyan student.chalmers.se> writes:
When should i use enforce instead of assert?
Dec 20 2013
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 20 December 2013 at 23:08:32 UTC, TheFlyingFiddle 
wrote:
 When should i use enforce instead of assert?
Assert is for things that is completely controlled by your program. If an assert fails, it is something you, the programmer, can fix by changing the code. enforce is for things that interact with the outside world at runtime. It might be fixable by the user, creating a file or something like that.
Dec 20 2013
prev sibling next sibling parent "Dicebot" <public dicebot.lv> writes:
Asserts are removed in release builds, enforces remain.

Asserts are used to verify internal sanity of application and 
never happen if it does not contain programming errors.

Enforces provide syntax sugar for routine error handling under 
normal workflow.
Dec 20 2013
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
TheFlyingFiddle:

 When should i use enforce instead of assert?
Others have already told you most of the answer. Let me add that enforce() is heavier for the compiler, so don't put an enforce in a performance-critical path in your code. Bye, bearophile
Dec 20 2013