Copy Constructor DIP

Andrei Alexandrescu SeeWebsiteForEmail at erdani.com
Fri Jul 13 16:02:51 UTC 2018


On 7/13/18 11:14 AM, Atila Neves wrote:
> On Friday, 13 July 2018 at 14:12:59 UTC, Andrei Alexandrescu wrote:
>> On 7/13/18 8:31 AM, Atila Neves wrote:
>>> On Friday, 13 July 2018 at 03:01:25 UTC, Manu wrote:
>>>> [...]
>>>
>>> https://github.com/search?q=%22this%5C%28ref%22+language%3AD&type=Code
>>>
>>> The answer seems to be: not many. Most of the results above are false 
>>> positives because github won't let me escape the left parenthesis.
>>
>> A proposal that just works without any user intervention would 
>> definitely be attractive.
>>
>> The drawback is silent modification of code behavior. Consider:
>>
>> import std.stdio;
>> struct A {
>>     this(ref immutable A obj) { writeln("x"); }
>> }
>> void main()
>> {
>>     immutable A a1;
>>     A a2 = A(a1);
>>     A a3 = a1;
>> }
>>
>> With the current language, "x" is printed once. If we make the ctor 
>> implicit, "x" is printed twice.
>>
>> In the "levels of hell" of semantics changes, probably the nicest is 
>> allowing code to compile that previously didn't. Then, refusing to 
>> compile code that previously did would be definitely bad. But if you 
>> want to drink the cup of disgrace to the bottom, you must reach for 
>> silent change of behavior.
> 
> I agree on the levels of hell. I wasn't aware the kind of change above 
> would happen - methinks it should be in the DIP.

Great. Razvan, can you please add this example with discussion to the 
DIP (probably at the end of the Motivation section as an explanation why 
we need the addition of @implicit). Thanks.

> I think I now even understand why `@implicit` is there to begin with. I 
> still think it's confusing, so imagine someone new to the language.

Interesting. Coming from a C++ background makes the matter obvious 
because there's plenty of implicit vs. explicit discussion. To 
compensate for my bias - what do you think would be a better attribute name?


Andrei



More information about the Digitalmars-d mailing list