May 21, 2022
On Sun, May 22, 2022 at 02:51:24AM +0000, forkit via Digitalmars-d wrote:
> On Saturday, 21 May 2022 at 03:05:08 UTC, Walter Bright wrote:
[...]
> > Asserts are *not* for validating program input. Please do not use them for that. They are for checking that the program's logic is correct. If an assert is tripped, it is a bug in the program, not a problem with user input.
> 
> I don't agree, that 'tripping an assert' == 'a bug in your program'.
[...]

That's because you're using it wrong.  Assert is for asserting an assumption that your program is in a particular state when you're about to execute some code.  E.g., after computing a square of a number, you assert that the result is positive, because the subsequent code, that assumes it's positive, will otherwise produce nonsensical results. I.e., assert is for verifying that your algorithm for computing the square is correct, and hasn't produced a nonsensical negative value. If the assertion fails to hold, that means your squaring algorithm has a bug.

For environmental things like unexpected user input or the OS returning an unexpected value, you should not use assert. You should use std.exception.enforce instead.


On Sun, May 22, 2022 at 02:54:02AM +0000, forkit via Digitalmars-d wrote:
> On Sunday, 22 May 2022 at 02:51:24 UTC, forkit wrote:
> > 
> > I'd use an assert where I don't want to handle any 'unexpected' conditions.
> > 
> 
> e.g. out of memory. no disk space left, etc...

You should not use assert for this. Use std.exception.enforce instead.


T

-- 
Obviously, some things aren't very obvious.