www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - good example of assert as expression ?

reply user1234 <user1234 12.de> writes:
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 ?

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 ?
Feb 03
next sibling parent reply "Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> writes:
On 04/02/2026 8:58 AM, 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 ?
 
 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).
Feb 03
parent reply user1234 <user1234 12.de> writes:
On Tuesday, 3 February 2026 at 20:01:47 UTC, Richard (Rikki) 
Andrew Cattermole wrote:
 On 04/02/2026 8:58 AM, user1234 wrote:
 [...]
&& 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).
Ah yes, OrOr. Shame on me.
Feb 03
parent reply user1234 <user1234 12.de> writes:
On Tuesday, 3 February 2026 at 20:03:31 UTC, user1234 wrote:
 On Tuesday, 3 February 2026 at 20:01:47 UTC, Richard (Rikki) 
 Andrew Cattermole wrote:
 On 04/02/2026 8:58 AM, user1234 wrote:
 [...]
&& 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).
Ah yes, OrOr. Shame on me.
Initial question still stands however.
Feb 03
parent Quirin Schroll <qs.il.paperinik gmail.com> writes:
On Tuesday, 3 February 2026 at 20:04:21 UTC, user1234 wrote:
 On Tuesday, 3 February 2026 at 20:03:31 UTC, user1234 wrote:
 On Tuesday, 3 February 2026 at 20:01:47 UTC, Richard (Rikki) 
 Andrew Cattermole wrote:
 On 04/02/2026 8:58 AM, user1234 wrote:
 [...]
&& 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).
Ah yes, OrOr. Shame on me.
Initial question still stands however.
In principle, for something like this: ```d uint isqrt(ulong x); uint isqrt(long x) => x < 0 ? assert(0, "must be >= 0") : isqrt(cast(ulong)x); ``` them, it’ll be much more useful because you don’t want to back off from a switch expression because one case (the default case usually) is (supposedly) unreachable. If `assert` were a statement, you’d have to do something clunky like `function int(){ assert(0); }()`.
Feb 09
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 2/3/26 20:58, 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 ?
auto x = c0 ? e0 : c1 ? e1 : c2 ? e2 : assert(0);
 I mean don't you think it's an obscure feature that could be removed ? 
Every feature can be removed. Should it be removed? No.
Feb 04
prev sibling parent reply Nick Treleaven <nick geany.org> writes:
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.
Feb 09
parent reply user1234 <user1234 12.de> writes:
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.
Feb 09
parent user1234 <user1234 12.de> writes:
On Monday, 9 February 2026 at 22:19:42 UTC, user1234 wrote:
 Usually assert as expression will not work because of implicit 
 convs.
A very classic example for me would be that you cannot manually check a dot-var LHS: ```d void main() { struct S { int m; } S* s; if ((s || assert(0)).m == 42) {} } ``` The idea is just to panic on `s.m`. That being said, I've read that that are plans for a new null-deref check.
Feb 16