good example of assert as expression ?

user1234 user1234 at 12.de
Mon Feb 9 22:19:42 UTC 2026


On Monday, 9 February 2026 at 21:41:03 UTC, Nick Treleaven wrote:
> 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.

during the other  topic Walter suggested that this is useful 
because

> is there because it is useful, as `if` statements cannot be put 
> inside an expression.

(https://forum.dlang.org/post/10lue34$2hvq$1@digitalmars.com).

excuse me but that never works. Excepted as sub very direct 
expression in a statment.

```d
struct S{}

void test(S*){}

void main()
{
     test(new S || assert(0));
}
```

Usually assert as expression will not work because of implicit 
convs.




More information about the Digitalmars-d mailing list