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

Manu turkeyman at gmail.com
Thu Aug 15 00:35:33 UTC 2024


I just wanted to share a brief discussion on a PR last night, because it's
something that's always bugged me, and there is a change in 2.111 that
tightens the rules for declaring auto ref such that the keywords must
appear next to eachother.

In the PR there was a piece of code that went `const auto ref T`, and I
suggested changing to `auto ref const T` as convention at least.

The reason I perceive is that this is valid syntax: auto ref
const(CowArray) rhs
This is not: const(auto ref CowArray) nhs

For me, it's a conceptual flow thing:
const auto ref T goes: type stuff (const) -> storage class stuff (auto ref)
-> type stuff (T)
auto ref const T goes: storage class stuff (auto ref) -> type stuff (const T
)

It feels crude to interleave type stuff and storage class stuff in an
arbitrary way.

The distinction feels more compelling when you consider that parens may
appear after type constructors (const(T)), and so in those cases, the code
MUST be written with the const immediately preceding the type, forcing the
storage class to the left.

Since it's invalid to put storage class stuff on the end (actual type must
be on the end), then I would mandate that storage class stuff should go at
the start, not randomly in the middle somewhere, ie: storage_class
type_constructor actual_type

If it were up to me, I would mandate this in the spec, but failing that, at
least proliferate this by general policy; I think this helps non-D coders
to osmose this complexity without a detailed conversation... and for
everyone else, the code just reads more consistently.

I see a surprising number of people writing `const ref T`, and I suspect
that people are writing it holding the same kind of conceptual confusion
that a lot of new users have expressed to me. As I see it, the fact people
tend to write this at all is clear evidence that we shouldn't allow it!


Similarly, allowing attributes on functions should likewise appear after
the function prototype:

struct S {
  const(T) method();
  const T method(); // not the same; synonym for `T method() const`
}

This kind of confusion is just totally unnecessary. Any non-expert reader
would assume they are the same. There's no value in inviting this confusion.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20240815/daa3b428/attachment.htm>


More information about the Digitalmars-d mailing list