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

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Aug 15 11:04:02 UTC 2024


On Thursday, August 15, 2024 4:34:49 AM MDT Richard (Rikki) Andrew Cattermole 
via Digitalmars-d wrote:
> On 15/08/2024 9:05 PM, IchorDev wrote:
> > Yeah, I feel you. When I first started writing in D I’d also use `const
> > ref`, but I agree that interleaving the storage class and the type is
> > not right. I’m not alone in having started with `const ref`, and then
> > having moved to using `ref const` as I understood the language better.
> > I think part of the reason some gravitate to `const ref` because of
> > ignorance when coming from C++, but I never used C++ because I was aware
> > of its reputation. Instead, I found the ordering to violate English
> > adjective ordering. `ref const T` sounds wrong in the same way as ‘red
> > big balloon’. In English you can have a ‘constant reference integer’ but
> > not a ‘reference constant integer’; it sounds like an imperative
> > instruction.
>
> +1
>
> Perhaps not an error in practice, but a warning is certainly warranted
> as it shows a lack of understanding in the language.

Not really. Unless you actually need to use parens, const ref is perfectly
valid, and I really don't see the problem. I understand const perfectly fine
and yet I always use const ref unless I have to use ref const, because ref
const is just plain ugly - and it doesn't add any value except in cases
where you can't use it anyway.

I agree that having function attributes be legal on both the left-hand and
right-hand side of a function is problematic, but I see no value in forcing
their order, and in general, making their order matter is just going to be
annoying to deal with.

Either way, warnings are a terrible idea in general. When you're dealing
with warnings, you end up in one of two situations:

1. You ignore the ones that don't actually need fixing, in which case, the
number of warnings will eventually grow to the point that you can't actually
see the ones that matter, making the warnings borderline useless.

2. You "fix" all of the warnings so that none are buried, meaning that
you're effectively treating all warnings as errors even though many of them
don't actually indicate a real problem, making the difference between
warnings and errors effectively pointless.

The fact that dmd even has warnings was a huge mistake IMHO. Either they
should be errors, or they should be left up to linters, and we should avoid
adding new warnings like the plague (honestly, I think that we should really
consider getting rid of them entirely).

And warnings in D are even worse than they are in most languages, because -w
is unfortunately a thing, meaning that adding a warning can result in an
error, which can not only make perfectly valid code cease to compile with a
compiler update, but it affects conditional compilation, because it affects
checks for whether a particular piece of code compiles, which is used
_heavily_ in templated code. So, you can end up with template constraints
which suddenly fail - or even worse, the code ends up compiling but using a
different branch, and you silently end up with worse performance (or maybe
even altered behavior in some cases).

If you want to have a linter of some kind warn about random stuff like const
ref, then I really don't care, but adding anything of the sort to the
compiler is a terrible idea.

- Jonathan M Davis






More information about the Digitalmars-d mailing list