The point of const, scope, and other attributes

H. S. Teoh hsteoh at quickfur.ath.cx
Fri May 13 18:43:11 UTC 2022


On Fri, May 13, 2022 at 06:25:22PM +0000, Fry via Digitalmars-d wrote:
> On Wednesday, 11 May 2022 at 18:18:18 UTC, Walter Bright wrote:
> > On 5/11/2022 3:48 AM, IGotD- wrote:
> > > C# went the other way, no const parameters at all.
[...]
> > It's not simpler for someone trying to read the code. Function
> > documentation almost never says "by the way, this `getValue`
> > function also tweaks the database."
> 
> Good documentation will. Really const doesn't help with making code
> more readable either. I avoided using const in D cause it just got in
> the way more than it was helping.

Yeah, more than one long-time D user (including myself) has come to this
conclusion. In my own experience, const is mainly useful at the lowest
levels of code (strings, leaf-node modules that operate on some
self-contained data structure that doesn't depend on anything else).
Once you get to a high enough level of abstraction, it starts getting in
your way and becomes far too much more work than the benefits it offers;
it's just not worth the trouble.  So these days I don't really bother
with const, except in small pockets of leaf-node code.


> The most useful feature for const to exist in C++ was that it allows
> you to do `const T&` which will allow you to pass anything to that
> function, rvalue or lvalue without making a copy. D doesn't do this
> though.
[...]

I thought `auto ref` was supposed to do this?  But I recall people
hating `auto ref` for various reasons...


T

-- 
Gone Chopin. Bach in a minuet.


More information about the Digitalmars-d mailing list