`const ref T` or `ref const T`?

IchorDev zxinsworld at gmail.com
Fri Aug 16 14:11:37 UTC 2024


On Thursday, 15 August 2024 at 17:59:59 UTC, Renato Athaydes 
wrote:
> I am trying to understand what is currently happening with D, 
> and where it is going.

That’s a complicated question. If you want a good answer, ask 
Walter and Átila, since it’s ultimately up to them what does or 
does not get added to the language.

> Someone briskly told me to stop using `in` because that's 
> "buggy" or something

No, it’s just in a state of uncertainty:
- In the canonical language right now it’s a way of making 
parameters `const`, but since it has to be written like a storage 
class it means you can’t write even the equivalent of (for 
example) `const(void)*`, so it’s completely redundant. It could 
be removed and no functionality would be taken away.
- With the preview switch it’s `scope ref const` that binds 
rvalues, which is useful when you want to take medium-sized 
structs by reference to reduce copying (rather than to mutate 
them). Since it’s behind a preview switch it’s not part of the 
canonical language, and may or may not ever become part of the 
canonical language at this point.
- A future proposal could replace both of these versions of `in` 
without much warning since they are rarely used.

> and I found [a comment from 
> 2018](https://forum.dlang.org/thread/dkstvxwbcluncgllvypt@forum.dlang.org) (wow, 6 years ago?!) explaining that there's controversy on what that should actually mean (`const` VS `const scope`) and the current docs mention it's just `const` unless you use the `-preview=in` flag

Yeah the fact that it’s remained a preview for so long isn’t 
promising. It’s like a feature graveyard.

> and does `scope` do what DIP1000 said it would or has it 
> changed? If it does, DIP1000 was then only "partially" 
> accepted??

Only if you use `-preview=dip1000`, otherwise `scope` is more of 
a promise than a compiler-enforced guarantee. A lot of us wanted 
DIP1000 to work out, but it hasn’t for a variety of reasons.

> I also saw somewhere that pointers should not be used in D 
> anymore in most cases because `ref` is a better and safer 
> alternative... is that a correct assessment?

Not really. Use whichever you prefer. Pointers will never bind to 
rvalues, whereas there’s a preview for `ref` binding to rvalues 
already. Pointers to structs require explicit `&`/`*`, `ref` lets 
you write the same code as for passing by value.


More information about the Digitalmars-d mailing list