const as default for variables

Sönke Ludwig via Digitalmars-d digitalmars-d at puremagic.com
Sun Mar 15 06:21:11 PDT 2015


Am 15.03.2015 um 08:44 schrieb Walter Bright:
> On 3/14/2015 10:23 PM, deadalnix wrote:
>> On Sunday, 15 March 2015 at 05:08:54 UTC, Walter Bright wrote:
>>> On 3/14/2015 8:50 PM, deadalnix wrote:
>>>> Const is the inconvenience of immutability without its benefit. That
>>>> would be a
>>>> poor default, IMO.
>>>
>>> It's utility is in self-documenting encapsulation.
>>>
>>> It also turns out that it offers optimization possibilities for ARC
>>> systems.
>>
>> You'll have to explain yourself, as there is nothing obvious here.
>>
>> I see how immutable can be leveraged to optimize, but I don't think
>> const offers
>> the same capabilities.
>
> "const ref" can tell the optimizer that that path for a ref counted
> object cannot alter its ref count.

My proposal would be to make "scope" (with strong recursive guarantees) 
the default instead to achieve that goal. That would give the compiler a 
lot of optimization potential (much more than just for RC), and would at 
the same time most probably be a lot less limiting. It would also be a 
nice documentation trait to see where references are stored for later 
use. And it could even be used to implement checked safety for calling 
C/C++ functions with GC references.

I agree with Dicebot that any such change should ideally be done as a 
single big step for all attributes at once. However, instead of a major 
version bump, we could have a normal deprecation path à la "opt-in only" 
-> "opt-in + opt-out" -> "opt-out only" -> remove old defaults.


More information about the Digitalmars-d mailing list