On Sat, 24 Aug 2024 at 10:11, Nicholas Wilson via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
On Friday, 23 August 2024 at 20:08:53 UTC, kinke wrote:
> I suggest adding a `core.builtins.expect()` intrinsic to
> standardize the existing LDC and GDC intrinsics, adding a dummy
> (`pragma(inline, true)`!) identity function for DMD. No need
> for introducing statement-level UDAs, other compiler magic,
> complicating language/spec etc. `core.attribute` already does
> this for magic UDAs.

https://github.com/dlang/dmd/pull/16807

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...