Copy Constructor DIP

Atila Neves atila.neves at gmail.com
Fri Jul 13 17:34:31 UTC 2018


On Friday, 13 July 2018 at 16:02:51 UTC, Andrei Alexandrescu 
wrote:
> 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?

I came to D via C++ as well. Maybe my brain short-circuited 
because I saw `A a3 = a1;` in a discussion about copy 
constructors and assumed it would be called, and didn't realise 
it wouldn't have been before. I just took for granted the 
implicitness of the constructor call, since that's what C++ does!

I wasn't even aware one could declare a C++ copy constructor 
`explicit` until I tried it just now. It's just not something 
I've ever thought about.

Now that I understand the implicit call `@implicit` makes sense 
to me but I'm convinced it'll confuse more people than not. I'll 
see if I can come up with a better name.

Atila


More information about the Digitalmars-d mailing list