Standard way to supply hints to branches

Nicholas Wilson iamthewilsonator at hotmail.com
Sat Aug 24 01:44:27 UTC 2024


On Saturday, 24 August 2024 at 01:26:43 UTC, Manu wrote:
> Thanks Nick! That's good to have.
>
> That said, I still feel using expect() is pretty invasive.
> If you have a bunch of existing code, marking it up is a fiddly
> transformation, and hurts readability:
>
> if (fails_validation)
>   return -1;
> if (check_for_lilely_case)
>   return v;
> return calculate(v);
>
>
> // invasive and pretty severely damages readibility
> if (expect(fails_validation, false))
>   return -1;
> if (expect(check_for_lilely_case, true))
>   return v;
> return calculate(v);
>
>
> // paste token on end of line, doesn't really affect 
> readibility at all
> if (fails_validation) @unlikely
>   return -1;
> if (check_for_lilely_case) @likely
>   return v;
> return calculate(v);
>
>
> An attribute attaching to control statements would definitely 
> be nice for my money...

note there is nothing stopping you from doing

```d
pragma (inline, true) bool unlikely(bool val) => expect(val, 
false);
pragma (inline, true) bool   likely(bool val) => expect(val, 
true);

if (unlikely(fails_validation))
   return -1;
if (check_for_liklely_case.likely) // if you feel that (ab)using 
UFCS is prettier
   return v;
return calculate(v)
```



More information about the Digitalmars-d mailing list