digitalmars.D - good example of assert as expression ?
- user1234 (19/19) Feb 03 Following a [previous
- Richard (Rikki) Andrew Cattermole (4/28) Feb 03 && isn't useful here, its ||
- user1234 (3/8) Feb 03 Ah yes, OrOr. Shame on me.
- user1234 (2/13) Feb 03 Initial question still stands however.
- Quirin Schroll (13/27) Feb 09 In principle, for something like this:
- Timon Gehr (6/10) Feb 04 auto x = c0 ? e0 :
- Nick Treleaven (14/16) Feb 09 Suppose there's a function `f` which takes a lazy parameter but
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
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
On Tuesday, 3 February 2026 at 20:01:47 UTC, Richard (Rikki) Andrew Cattermole wrote:On 04/02/2026 8:58 AM, user1234 wrote:Ah yes, OrOr. Shame on me.[...]&& 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
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:Initial question still stands however.On 04/02/2026 8:58 AM, user1234 wrote:Ah yes, OrOr. Shame on me.[...]&& 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
On Tuesday, 3 February 2026 at 20:04:21 UTC, user1234 wrote:On Tuesday, 3 February 2026 at 20:03:31 UTC, user1234 wrote: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); }()`.On Tuesday, 3 February 2026 at 20:01:47 UTC, Richard (Rikki) Andrew Cattermole wrote:Initial question still stands however.On 04/02/2026 8:58 AM, user1234 wrote:Ah yes, OrOr. Shame on me.[...]&& 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 09
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
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
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:during the other topic Walter suggested that this is useful becauseFollowing 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.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
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









Quirin Schroll <qs.il.paperinik gmail.com> 