Copy Constructor DIP

Atila Neves atila.neves at gmail.com
Fri Jul 13 15:14:06 UTC 2018


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.

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.

Atila


More information about the Digitalmars-d mailing list