Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 20, 2013 assert vs enforce. | ||||
---|---|---|---|---|
| ||||
When should i use enforce instead of assert? |
December 20, 2013 Re: assert vs enforce. | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheFlyingFiddle | 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 Re: assert vs enforce. | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheFlyingFiddle | 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 Re: assert vs enforce. | ||||
---|---|---|---|---|
| ||||
Posted in reply to TheFlyingFiddle | 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
|
Copyright © 1999-2021 by the D Language Foundation