Thread overview
assert vs enforce.
Dec 20, 2013
TheFlyingFiddle
Dec 20, 2013
Adam D. Ruppe
Dec 20, 2013
Dicebot
Dec 20, 2013
bearophile
December 20, 2013
When should i use enforce instead of assert?
December 20, 2013
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.
December 20, 2013
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.
December 20, 2013
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