good example of assert as expression ?

Nick Treleaven nick at geany.org
Mon Feb 9 21:41:03 UTC 2026


On Tuesday, 3 February 2026 at 19:58:14 UTC, user1234 wrote:
> Following a [previous 
> thread](https://forum.dlang.org/thread/loojkahtbzigawrscmsr@forum.dlang.org), can you give me good example where the assert expression is useful ?

Suppose there's a function `f` which takes a lazy parameter but 
you expect it will not be evaluated, e.g. because of the other 
arguments passed. The function does something useful before/after 
conditionally evaluating its lazy parameter. To test your 
assumption, you can pass an assert expression as the lazy 
argument:
```d
R f(Args, lazy T v);
...
R r = f(args, assert(0));
```
Otherwise you'd need to pass `() { assert(0); }()` which is a bit 
ugly.


More information about the Digitalmars-d mailing list