good example of assert as expression ?
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Tue Feb 3 20:01:47 UTC 2026
On 04/02/2026 8:58 AM, user1234 wrote:
> Following a [previous thread](https://forum.dlang.org/thread/
> loojkahtbzigawrscmsr at forum.dlang.org), can you give me good example
> where the assert expression is useful ?
>
> So far I only see the case where it is used as AndAnd RHS.
>
> ```d
> void main()
> {
> auto lhs()
> {
> extern(C) int rnd;
> return (rnd % 1) + 1;
> }
>
> lhs() && assert(0);
> }
> ```
>
> I remember an old NG topic where someone asked if D supports the "assert
> idiom".
>
> So seriously assert as an expression just works in this context ?
> I mean don't you think it's an obscure feature that could be removed ?
&& isn't useful here, its ||
I.e. we use it in the form of ``ptr || assert(0)`` for the null check
(not exact but close enough).
More information about the Digitalmars-d
mailing list