Head Const
Shachar Shemesh via Digitalmars-d
digitalmars-d at puremagic.com
Fri Feb 19 00:51:26 PST 2016
On 17/02/16 14:03, Jakob Ovrum wrote:
> On Monday, 15 February 2016 at 22:48:16 UTC, Walter Bright wrote:
>> rears its head again :-)
>>
>> Head Const is what C++ has for const, i.e. it is not transitive,
>> applies to one level only. D has transitive const.
>>
>> What head const will do for us:
>>
>> 1. make it easy to interface to C++ code that uses const, as currently
>> it is not very practical to do so, you have to resort to pragma(mangle)
>>
>> 2. supports single assignment style of programming, even if the data
>> is otherwise mutable
>>
>> The downside is, of course, language complexity.
>
> How about disallowing immutable data with extern(C++) types? With
> extern(C++) data always mutable, `const` could safely be reused to mean
> C++ const in bindings. Any `mutable`-style code would be implemented in
> C++.
>
The way I see it, here's how it is with C++:
Get const pointer. Cast away its constness. If the original data was
mutable, you're gold. If not, undefined behaviour.
This is how it works with D:
You're not allowed to cast away constness (UB). The syntax still allows
you to do it, and because of the very rigid way in which const works in
D, you often have no choice but to do so in practice. The compiler tries
not to break your code, so if the original data was mutable, you should,
probably, be okay.
I fail to see how the C++ way is any worse than ours.
Shachar
More information about the Digitalmars-d
mailing list