August 18, 2012
On Friday, 17 August 2012 at 22:05:56 UTC, Walter Bright wrote:
> On 8/17/2012 2:34 PM, Mehrdad wrote:
>> On Friday, 17 August 2012 at 20:46:02 UTC, Walter Bright wrote:
>>> On 8/16/2012 6:43 PM, Mehrdad wrote:
>>>> On Friday, 17 August 2012 at 01:25:18 UTC, Chris Cain wrote:
>>>>> Yeah. Again, you can't modify __the const view__.
>>>>
>>>> Isn't that kinda useless, if it tells you nothing about the object itself?
>>>
>>> It means you can write code that can process both mutable and immutable objects.
>>
>> I meant from a C++/D comparison standpoint...
>
> I don't know what you're driving at.

He wants to know what optimizations you get from transitive const over C++ const when you ignore other D features such as immutable and pure.
August 18, 2012
On Saturday, 18 August 2012 at 04:18:33 UTC, Jesse Phillips wrote:
> On Friday, 17 August 2012 at 22:05:56 UTC, Walter Bright wrote:
>> On 8/17/2012 2:34 PM, Mehrdad wrote:
>>> On Friday, 17 August 2012 at 20:46:02 UTC, Walter Bright wrote:
>>>> On 8/16/2012 6:43 PM, Mehrdad wrote:
>>>>> On Friday, 17 August 2012 at 01:25:18 UTC, Chris Cain wrote:
>>>>>> Yeah. Again, you can't modify __the const view__.
>>>>>
>>>>> Isn't that kinda useless, if it tells you nothing about the object itself?
>>>>
>>>> It means you can write code that can process both mutable and immutable objects.
>>>
>>> I meant from a C++/D comparison standpoint...
>>
>> I don't know what you're driving at.
>
> He wants to know what optimizations you get from transitive const over C++ const when you ignore other D features such as immutable and pure.

In D, const without immutable is meaningless.

const on its own provides no guarantees, it just imposes restrictions so that immutable can provide guarantees.

Think of it this way: const without immutable is like an interface with only one implementation. It's pointless: using the interface would just deny you access to other details of the (only) implementation. However, the restrictions provided by the interface (i.e. that the implementations must have certain methods) allows other implementations, and then using the interface is a useful way of writing polymorphic code.

const is the interface to mutable and immutable.
August 18, 2012
On Saturday, August 18, 2012 13:25:56 Peter Alexander wrote:
> In D, const without immutable is meaningless.

That's not quite true. There _are_ cases where const by itself is enough to provide guarantees (and I give such an example elsewhere in this thread). However, the situations where that's the case are quite limited, because the compiler has to be able to know that no other references to that data exist. With pure added in, they increase considerably, and of course, with immutable, they're much, much higher, because it doesn't have to worry about figuring out whether other, mutable references to the data exist or not. But if the compiler knows that no other references to const data exist, then that's enough to make optimizations (though whether the compiler ever _does_ do that is another matter entirely).

- Jonathan M Davis
August 18, 2012
On 8/18/2012 9:59 AM, Jonathan M Davis wrote:
> That's not quite true. There _are_ cases where const by itself is enough to
> provide guarantees (and I give such an example elsewhere in this thread).
> However, the situations where that's the case are quite limited, because the
> compiler has to be able to know that no other references to that data exist.
> With pure added in, they increase considerably, and of course, with immutable,
> they're much, much higher, because it doesn't have to worry about figuring out
> whether other, mutable references to the data exist or not. But if the
> compiler knows that no other references to const data exist, then that's
> enough to make optimizations (though whether the compiler ever _does_ do that
> is another matter entirely).

You're quite right.


August 18, 2012
On Saturday, 18 August 2012 at 11:26:01 UTC, Peter Alexander wrote:
> In D, const without immutable is meaningless.

lol, that was the whole point of this question... the whole point of Jon's example was to show it's not meaningless even without immutable. :)
August 18, 2012
On Saturday, 18 August 2012 at 20:22:59 UTC, Mehrdad wrote:
> On Saturday, 18 August 2012 at 11:26:01 UTC, Peter Alexander wrote:
>> In D, const without immutable is meaningless.
>
> lol, that was the whole point of this question... the whole point of Jon's example was to show it's not meaningless even without immutable. :)

Jon is right. If you can guarantee that there are no other references then const can provide some guarantees, but in general you don't know if there are other references.
August 18, 2012
On Saturday, 18 August 2012 at 22:04:21 UTC, Peter Alexander wrote:
> On Saturday, 18 August 2012 at 20:22:59 UTC, Mehrdad wrote:
>> On Saturday, 18 August 2012 at 11:26:01 UTC, Peter Alexander wrote:
>>> In D, const without immutable is meaningless.
>>
>> lol, that was the whole point of this question... the whole point of Jon's example was to show it's not meaningless even without immutable. :)
>
> Jon is right. If you can guarantee that there are no other references then const can provide some guarantees, but in general you don't know if there are other references.

Right, most optimizations are not applicable to general cases.
August 18, 2012
On 8/18/2012 4:28 PM, Mehrdad wrote:
> Right, most optimizations are not applicable to general cases.

That's pretty much always true with optimizations.
August 19, 2012
On Saturday, 18 August 2012 at 11:26:01 UTC, Peter Alexander wrote:

> const on its own provides no guarantees, it just imposes restrictions so that immutable can provide guarantees.

While in context with the original question this is fine, but I do not like this use of guarantee.

What I mean is, const does provide guarantees by itself. And it provides more than C++ because it is transitive and modifying a const reference is undefined.
August 19, 2012
On Sunday, 19 August 2012 at 19:26:58 UTC, Jesse Phillips wrote:
> On Saturday, 18 August 2012 at 11:26:01 UTC, Peter Alexander wrote:
>
>> const on its own provides no guarantees, it just imposes restrictions so that immutable can provide guarantees.
>
> While in context with the original question this is fine, but I do not like this use of guarantee.
>
> What I mean is, const does provide guarantees by itself. And it provides more than C++ because it is transitive and modifying a const reference is undefined.

What guarantees does const provide on its own?