Thread overview
D constness: head & tail
Mar 02, 2015
drug
Mar 02, 2015
Jonathan M Davis
Mar 02, 2015
drug
Mar 02, 2015
Jonathan M Davis
Mar 02, 2015
drug
Mar 02, 2015
ketmar
Mar 02, 2015
Jonathan M Davis
Mar 02, 2015
Tobias Pankrath
Mar 02, 2015
ketmar
March 02, 2015
what is the state of head&tail constness in D? Where we are and where we are moving toward?
Where can I find some info about head and tail constness? I mean forum posts, stackoverflow questions, arcticles somewhere and so on.

Thank in advance
March 02, 2015
On Monday, March 02, 2015 11:21:32 drug via Digitalmars-d-learn wrote:
> what is the state of head&tail constness in D? Where we are and where we
> are moving toward?
> Where can I find some info about head and tail constness? I mean forum
> posts, stackoverflow questions, arcticles somewhere and so on.

There is no head-const in D. const is transitive, so you can never have anything which is const with anything mutable inside it (other references to the same data might refer to it as mutable, but never via any const reference or pointer). However, tail-const is sometimes possible. For instance, when slicing arrays, you get a tail-const slice (the resulting array is mutable, but the elements themselves are still const). For pointers, it's simply a matter of where you put the parens.

const T* foo;
const(T*) foo

both declare a const pointer to a const T, whereas

const(T)* foo

declares a mutable pointer to a const T. But something like

T const(*) foo;

is illegal, becaues it would violate the transivity of const.

For classes, we have std.typecons.Rebindable
(http://dlang.org/phobos/std_typecons.html#Rebindable):

Rebindable!(const T) foo;

Because there is no way to represent a class object separately from its reference in D, there is no way to indicate that a reference should be mutable while the class object itself is const, but Rebindable takes care of that by using a wrapper struct so that the reference can still be const, but the wrapper itself isn't.

There are mutiple places in the online documentation which discuss const, e.g.

http://dlang.org/const3.html http://dlang.org/const-faq.html

There are also several questions on stackoverflow which discuss it. A good one to look at would be

http://stackoverflow.com/questions/4219600/logical-const-in-d

I also expect that Ali's book discusses it, but I'd have to go digging through it to find it, and you'd probably be better served reading through it yourself anyway:

http://ddili.org/ders/d.en/index.html

In any case, AFAIK, where we are with const will be where we will always be with const in D. It's not something that's generally considered to need more work. There are some folks who would like to see a way to do Rebindable built into the language, but that's never happened, and I don't really expect it to. But other than that, there really hasn't been much discussion on changing how const works any time recently. So, "where we're moving toward" is pretty much where we are now.

- Jonathan M Davis

March 02, 2015
or, tl;dr: `const` in D is working as it was designed to work. there are no changes planning for it, and it will not be turned to "c++-like" const.

March 02, 2015
I'm just trying to study constness in D and periodically fails with it. Thanks for answer.
Then I guess that constness in D is in the finished form, hasn't some pitfalls, won't be changed significantly in some future and so I just need to learn it and understand it?
March 02, 2015
On Monday, March 02, 2015 10:15:00 ketmar via Digitalmars-d-learn wrote:
> or, tl;dr: `const` in D is working as it was designed to work. there are no changes planning for it, and it will not be turned to "c++-like" const.

We couldn't even if we wanted to. It would never work with immutable.

- Jonathan M Davis

March 02, 2015
On Monday, 2 March 2015 at 10:15:00 UTC, ketmar wrote:
> or, tl;dr: `const` in D is working as it was designed to work. there are
> no changes planning for it, and it will not be turned to "c++-like" const.

It's working as it's designed to work, although the design is somewhat lacking:

https://github.com/D-Programming-Language/phobos/blob/master/std/container/array.d#L505
March 02, 2015
On Monday, March 02, 2015 14:20:45 drug via Digitalmars-d-learn wrote:
> I'm just trying to study constness in D and periodically fails with it.
> Thanks for answer.
> Then I guess that constness in D is in the finished form, hasn't some
> pitfalls, won't be changed significantly in some future and so I just
> need to learn it and understand it?

Pretty much, yeah. const in D is what it's going to be. That involves some pros and cons in comparison to what C++ has, so in some ways it's going to be better, and in others, it's going to be frustrating in its restrictions (e.g. it doesn't work with anything that would require the mutable keyword in C++), but it's the way that it's going to be in D.

- Jonathan M Davis

March 02, 2015
On Mon, 02 Mar 2015 11:33:00 +0000, Tobias Pankrath wrote:

> It's working as it's designed to work, although the design is somewhat lacking:

yeah, i didn't mean that the design is best possible one, but it's set in stone for now.

March 02, 2015
On 02.03.2015 14:51, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Monday, March 02, 2015 14:20:45 drug via Digitalmars-d-learn wrote:
>> I'm just trying to study constness in D and periodically fails with it.
>> Thanks for answer.
>> Then I guess that constness in D is in the finished form, hasn't some
>> pitfalls, won't be changed significantly in some future and so I just
>> need to learn it and understand it?
>
> Pretty much, yeah. const in D is what it's going to be. That involves some
> pros and cons in comparison to what C++ has, so in some ways it's going to
> be better, and in others, it's going to be frustrating in its restrictions
> (e.g. it doesn't work with anything that would require the mutable keyword
> in C++), but it's the way that it's going to be in D.
>
> - Jonathan M Davis
>
Yes, const in D is both exciting and frustrating thing. Thanks again.