Want reasonable reference counting? Disable automatic sharing of immutable
H. S. Teoh
hsteoh at quickfur.ath.cx
Sun Nov 14 20:46:59 UTC 2021
On Sun, Nov 14, 2021 at 09:22:39PM +0100, Timon Gehr via Digitalmars-d wrote:
> On 14.11.21 21:04, H. S. Teoh wrote:
> >
> > So, either (1) we cannot use const/in in generic functions, or (2)
> > ref-counted objects cannot be used in generic code.
> >
> > Given D's emphasis on generic code, (1) seems like the only viable
> > option. Which makes const in D even narrower in scope than ever.
>
> I don't agree that it changes anything for generic code. `const`
> already prevents useful patterns such as lazy initialization.
Yes, which already confines const to a very narrow scope indeed. And if
we now make ref-counting require mutable, then const is basically
relegated to the niche of niches, usable only for very narrow and
specific use cases like const(char)[] or immutable(char)[] aka string.
As I've mentioned before, while const/immutable do provide very strong
guarantees, they are also very narrow in scope, such that IME they are
rarely useful outside of bottom-level types (i.e., leaf nodes in the
graph of type dependencies). Outside of that, they're so restrictive
that they aren't really applicable most of the time. They are useful
only 1 or 2 levels above PODs, anything else and you start running into
problems.
It seems wasteful to allocate a large part of the type system to
something so limited in applicability in real-life code.
> The issue is that despite all that, people still think D const is
> similar to C++ const. It's really not. Notions such as "const
> correctness" are misguided in D.
C++ const is a joke, I'm not even thinking about it. :-D
The thing is that `in` is touted (in tutorials, documentation, and
sometimes in advice to D newbies) as a good practice for function
parameters when the function is not modifying data. In practice,
however, this is only applicable to PODs and aggregates 1 or 2 levels
above that. Beyond that `in` basically adds no value, since as you
mentioned, it excludes a lot of useful idioms like lazy initialization,
caching, and now ref-counting.
An entire keyword, spent on a feature that can only be used in code of
limited complexity.
T
--
Государство делает вид, что платит нам зарплату, а мы делаем вид, что работаем.
More information about the Digitalmars-d
mailing list