cast(function qualifier)

Paul Backus snarwin at gmail.com
Sat Feb 15 13:28:38 UTC 2025


On Wednesday, 5 February 2025 at 11:22:59 UTC, Quirin Schroll 
wrote:
> I generally dislike the qualifier casts because they override 
> all existing qualifiers.
> * `cast(qualifiers)` removes all qualifiers, then adds the 
> given qualifiers (includes `cast()`).
>
> It’s a blunt tool that makes making precise cuts hard. You 
> locked a mutex and and now are good to go with casting away 
> `shared`? Well, too bad, you did `cast()` and also removed a 
> `const`, welcome to UB town. You made sure some C API doesn’t 
> mutate your stuff and cast away `const`? Well, too bad, you 
> also cast away `shared` and now you’re debugging race 
> conditions.
>
> My idea is to use `...` to abbreviate the type of the operand 
> within the `cast`; that would make `cast(...)` a no-op.
> Qualifier manipulations go in front of `...` and (member) 
> function attribute manipulations trail behind. `cast(const ... 
> const) dg` adds(!) `const` both as a qualifier and a member 
> function attribute to the type of `dg`.
>
[...]
>
> ```d
> shared inout void delegate() @safe const dg;
>
> cast() dg; // void delegate() const @safe
> cast(!shared ...) dg; // inout void delegate() const @safe
> cast(-shared ...) dg; // inout void delegate() const @safe
> cast(-const ...) dg; // error, `dg` isn’t const
> cast(const ...) dg; // const inout shared void delegate() const 
> @safe
> cast(... !const) dg; // inout shared void delegate() @safe
> cast(... !@safe) dg; // inout shared void delegate() const
> cast(... !pure) dg; // inout shared void delegate() const @safe
> cast(... -pure) dg; // error, `dg` isn’t pure
> cast(... pure nothrow) dg; // const inout shared void 
> delegate() const nothrow pure @safe
> ```

I understand where you're coming from but unfortunately the 
result looks like line noise.

Even if it's more verbose, I would much rather read code like the 
following:

```d
alias Dg = typeof(dg);
cast(Unshared!Dg) dg; // inout void delegate() const @safe
cast(UnconstContext!Dg) dg; // inout shared void delegate() @safe
cast(Unsafe!Dg) dg; // inout shared void delegate() const
cast(Impure!Dg) dg; // inout shared void delegate() const @safe
cast(Pure!(Nothrow!Dg)) dg; // const inout shared void delegate() 
const nothrow pure @safe
```


More information about the dip.ideas mailing list