Constant relationships between non-constant objects

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Tue Jun 17 22:59:20 PDT 2014


On Wed, 18 Jun 2014 02:26:24 +0000
Sebastian Unger via Digitalmars-d <digitalmars-d at puremagic.com> wrote:

> On Wednesday, 18 June 2014 at 02:15:21 UTC, Jesse Phillips wrote:
> > On Wednesday, 18 June 2014 at 01:31:33 UTC, Sebastian Unger
> > wrote:
> >> Or has D really done away with the MOST important use case of
> >> const (preventing developer mistakes! Not optimization.)
> >>
> >> Cheers,
> >> Seb
> >
> > Yes. D does not provide head-const.
> >
> > Though I would disagree that it is the most important use-case
> > for const.
>
> But can you agree that it is AN important use case?

It would be better to have it as an option, but in my experience, head-const
is close to useless, so while it might be nice to have upon occasion, not
having it isn't all that big a loss. I always thought that it was pretty
useless that Java provided head-const (especially because that's the _only_
kind of const that it has).

> If so, what was the rationale to not include a feature in D that has been
> used with great success in other languages? Don't get me wrong, I
> really like immutable and const and the fact that they are
> transitive. There are good use cases for this.

It comes down to the same reason that we have std.typecons.Rebindable to make
it so that you can have a non-const reference to a const object. The type
system doesn't really have the concept of a class, just a class reference.
When you say

MyClass c;

the type of MyClass is a reference to the class that's named MyClass rather
than being MyClass itself, and there's nowhere in the type system that makes
the distinction, because there's nowhere that you can even refer to a class
type without a reference to an object of that class type.

When const was first being worked on in D2, other options were explored with
regards to how to handle const, and for a short while we had a distinction
between head-const and tail-const when declaring variables of various types,
but it was ultimately decided that the result was just too complicated, and
the current route of using parens was introduced, which does lose us some
flexibility, but it gets us most of the way there while being much simpler.

However, I don't know think we ever really had head-const or tail-const
classes even then, because of how classes are represented in the compiler.
Walter attempted to rectify the problem multiple times but gave up on it,
since apparently, it's quite difficult, and he got sick of trying. At least
one major attempt has been made by another developer, but Walter didn't
approve of the way that it was done, so it never made it into the compiler or
the language (I don't know what the problems with it were though).

So, to fix this, we'd need a way (syntactically) to somehow indicate that a
class reference is head-const or tail-const without adding much complication
to the language, and there would have to be a major overhaul of how classes
are viewed by the compiler, which is enough of a pain that it's just never
been fully, successfully done.

So, we have std.typecons.Rebindable in order to get tail-const class
references, and elsewhere in this thread, Meta provided a similar wrapper to
give you head-const. However, no one has ever found head-const to be
important enough to put anything like that in Phobos. It's always the lack of
tail-const class references that folks complain about. You're the only person
I've ever seen who's complained about the lack of head-const.

- Jonathan M Davis


More information about the Digitalmars-d mailing list