digitalmars.D.learn - assert vs enforce.
- TheFlyingFiddle (1/1) Dec 20 2013 When should i use enforce instead of assert?
- Adam D. Ruppe (8/9) Dec 20 2013 Assert is for things that is completely controlled by your
- Dicebot (5/5) Dec 20 2013 Asserts are removed in release builds, enforces remain.
- bearophile (6/7) Dec 20 2013 Others have already told you most of the answer. Let me add that
When should i use enforce instead of assert?
Dec 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.
Dec 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.
Dec 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
Dec 20 2013