Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 07, 2008 What's this? (Was: DMD 1.028 and 2.012 releases) | ||||
---|---|---|---|---|
| ||||
Did you notice this example in docs? (http://www.digitalmars.com/d/2.0/struct.html#AssignOverload) S* opAssign(S s) { S tmp <== *this; // bitcopy *this into tmp *this <== s; // bitcopy s into *this tmp.~this(); // call destructor on tmp return this; } It seems to be something new in D 2.012. Does it work? How? BR Marcin Kuszczak (aarti_pl) |
March 07, 2008 Re: What's this? (Was: DMD 1.028 and 2.012 releases) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aarti_pl | Aarti_pl wrote:
> Did you notice this example in docs?
> (http://www.digitalmars.com/d/2.0/struct.html#AssignOverload)
>
>
> S* opAssign(S s)
> { S tmp <== *this; // bitcopy *this into tmp
> *this <== s; // bitcopy s into *this
> tmp.~this(); // call destructor on tmp
> return this;
> }
>
> It seems to be something new in D 2.012. Does it work? How?
It's pseudo-code. There is no <== operator nor .~this() in D. I probably made a mistake in presenting it this way, but I was trying to show what was going on.
The comments explain what is happening.
|
March 07, 2008 Re: What's this? (Was: DMD 1.028 and 2.012 releases) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | Walter Bright pisze:
> Aarti_pl wrote:
>> Did you notice this example in docs?
>> (http://www.digitalmars.com/d/2.0/struct.html#AssignOverload)
>>
>>
>> S* opAssign(S s)
>> { S tmp <== *this; // bitcopy *this into tmp
>> *this <== s; // bitcopy s into *this
>> tmp.~this(); // call destructor on tmp
>> return this;
>> }
>>
>> It seems to be something new in D 2.012. Does it work? How?
>
> It's pseudo-code. There is no <== operator nor .~this() in D. I probably made a mistake in presenting it this way, but I was trying to show what was going on.
>
> The comments explain what is happening.
Yes, such a pseudo-code should probably be avoided.
I thought that there was silently introduced special copy operator.
... the one that would disambiguate ref copying from object copying for example :-)
BR
Marcin Kuszczak
(aarti_pl)
|
March 07, 2008 Re: What's this? (Was: DMD 1.028 and 2.012 releases) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aarti_pl | Aarti_pl wrote:
> Walter Bright pisze:
>> Aarti_pl wrote:
>>> Did you notice this example in docs?
>>> (http://www.digitalmars.com/d/2.0/struct.html#AssignOverload)
>>>
>>>
>>> S* opAssign(S s)
>>> { S tmp <== *this; // bitcopy *this into tmp
>>> *this <== s; // bitcopy s into *this
>>> tmp.~this(); // call destructor on tmp
>>> return this;
>>> }
>>>
>>> It seems to be something new in D 2.012. Does it work? How?
>>
>> It's pseudo-code. There is no <== operator nor .~this() in D. I probably made a mistake in presenting it this way, but I was trying to show what was going on.
>>
>> The comments explain what is happening.
>
> Yes, such a pseudo-code should probably be avoided.
>
> I thought that there was silently introduced special copy operator.
> ... the one that would disambiguate ref copying from object copying for example :-)
Ohh that's not real code? That is confusing. Just put the pseudocode into comments then it won't be as misleading.
--bb
|
March 07, 2008 Re: What's this? (Was: DMD 1.028 and 2.012 releases) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Aarti_pl | "Aarti_pl" wrote in message
> Did you notice this example in docs?
> (http://www.digitalmars.com/d/2.0/struct.html#AssignOverload)
>
>
> S* opAssign(S s)
> { S tmp <== *this; // bitcopy *this into tmp
> *this <== s; // bitcopy s into *this
> tmp.~this(); // call destructor on tmp
> return this;
> }
>
> It seems to be something new in D 2.012. Does it work? How?
>
> BR
> Marcin Kuszczak
> (aarti_pl)
After reading this new feature, I'm really confused. I like the feature, but I'm not sure how it works, or why there are two functions for the same thing.
How does one use the postblit, and how does one use the opAssign? It seems they both serve the same function, but opAssign allows more control possibly? And does opAssign get called if the postblit doesn't exist or vice versa?
Not to knock what you have done, but it would seem to me more reasonable to have a single method, and if you wanted to blit the values before your custom code, just do:
S* opAssign(ref const S s)
{
*this = s;
...
}
And this would get called on both copy construction and assignment. The way I see it now (if each are individual operations), blitting won't work if the source is const and contains a pointer, so that the following confusing situation happens:
struct S
{
int[] buf
this(this)
{
buf = buf.dup;
}
S* opAssign(ref const S s)
{
buf = s.buf.dup;
return this;
}
}
const S s;
S t = s; // fails because the blit fails
S u;
u = s; // works because no blit occurred
Also it appears that your "equivalent" statement is wrong, or else the opAssign signature is wrong:
t = S.opAssign(s);
Doesn't t need to be a struct pointer? Or else is opAssign not really returning a struct pointer? Does this work?
S u;
u = t = s;
Finally, I don't understand the reasoning for tmp. It appears to be an unused part of the default opAssign...
-Steve
|
March 07, 2008 Re: What's this? (Was: DMD 1.028 and 2.012 releases) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Steven Schveighoffer pisze:
> After reading this new feature, I'm really confused. I like the feature, but I'm not sure how it works, or why there are two functions for the same thing.
Well, it occurred that this example was just a strange kind of "pseudo code" from Walter (please read the whole thread). I hope he will changed it in docs as it is really confusing.
Best Regards
Marcin Kuszczak
(aarti_pl)
|
Copyright © 1999-2021 by the D Language Foundation