good example of assert as expression ?
Quirin Schroll
qs.il.paperinik at gmail.com
Mon Feb 9 11:37:44 UTC 2026
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);
```
If D gets expression-based switch similar to how C# and Java have
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); }()`.
More information about the Digitalmars-d
mailing list