| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
November 26, 2013 Comparison | ||||
|---|---|---|---|---|
| ||||
Isn't it a bug that the assertion is triggered for this:
class Test3 {}
void main()
{
assert( (new Test3()) == (new Test3()) );
}
Tried it on http://dpaste.dzfl.pl/ as well.
/Jonas
| ||||
November 26, 2013 Re: Comparison | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jonas Drewsen | On Tuesday, 26 November 2013 at 21:37:49 UTC, Jonas Drewsen wrote: > Isn't it a bug that the assertion is triggered for this: > > class Test3 {} > > void main() > { > assert( (new Test3()) == (new Test3()) ); > } > > Tried it on http://dpaste.dzfl.pl/ as well. > > /Jonas Because your Test3 class has probably no own opEquals method, your comparison is the same as assert( (new Test3()) is (new Test3()) ); which is obviously wrong. See: https://github.com/D-Programming-Language/druntime/blob/master/src/object_.d#L116 | |||
November 27, 2013 Re: Comparison | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Namespace | On 11/26/13 7:00 PM, Namespace wrote:
> On Tuesday, 26 November 2013 at 21:37:49 UTC, Jonas Drewsen wrote:
>> Isn't it a bug that the assertion is triggered for this:
>>
>> class Test3 {}
>>
>> void main()
>> {
>> assert( (new Test3()) == (new Test3()) );
>> }
>>
>> Tried it on http://dpaste.dzfl.pl/ as well.
>>
>> /Jonas
>
> Because your Test3 class has probably no own opEquals method, your
> comparison is the same as
> assert( (new Test3()) is (new Test3()) );
> which is obviously wrong.
>
> See:
> https://github.com/D-Programming-Language/druntime/blob/master/src/object_.d#L116
>
The ddoc for the method is wrong. It should say something like:
returns != 0 if this object has the same memory address as obj. Subclasses may override this method to provide custom equality comparison.
Because for the OP, Test3 has the same contents as the other Test3 (both empty).
| |||
November 28, 2013 Re: Comparison | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | On Wednesday, 27 November 2013 at 00:46:30 UTC, Ary Borenszweig wrote:
> On 11/26/13 7:00 PM, Namespace wrote:
>> On Tuesday, 26 November 2013 at 21:37:49 UTC, Jonas Drewsen wrote:
>>> Isn't it a bug that the assertion is triggered for this:
>>>
>>> class Test3 {}
>>>
>>> void main()
>>> {
>>> assert( (new Test3()) == (new Test3()) );
>>> }
>>>
>>> Tried it on http://dpaste.dzfl.pl/ as well.
>>>
>>> /Jonas
>>
>> Because your Test3 class has probably no own opEquals method, your
>> comparison is the same as
>> assert( (new Test3()) is (new Test3()) );
>> which is obviously wrong.
>>
>> See:
>> https://github.com/D-Programming-Language/druntime/blob/master/src/object_.d#L116
>>
>
> The ddoc for the method is wrong. It should say something like:
>
> returns != 0 if this object has the same memory address as obj. Subclasses may override this method to provide custom equality comparison.
>
> Because for the OP, Test3 has the same contents as the other Test3 (both empty).
"does have the same contents as obj"
yes this is what confused me... and actually, it is a bit wierd coming from a c++ background that obj1 == obj2 is the same as (obj1 is obj2) and not per field comparison as expected (and documented). What is the reasoning behind this... principle of most astonishment? :)
| |||
November 29, 2013 Re: Comparison | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jonas Drewsen | On Thursday, 28 November 2013 at 13:23:22 UTC, Jonas Drewsen wrote:
> On Wednesday, 27 November 2013 at 00:46:30 UTC, Ary Borenszweig wrote:
>> On 11/26/13 7:00 PM, Namespace wrote:
>>> On Tuesday, 26 November 2013 at 21:37:49 UTC, Jonas Drewsen wrote:
>>>> Isn't it a bug that the assertion is triggered for this:
>>>>
>>>> class Test3 {}
>>>>
>>>> void main()
>>>> {
>>>> assert( (new Test3()) == (new Test3()) );
>>>> }
>>>>
>>>> Tried it on http://dpaste.dzfl.pl/ as well.
>>>>
>>>> /Jonas
>>>
>>> Because your Test3 class has probably no own opEquals method, your
>>> comparison is the same as
>>> assert( (new Test3()) is (new Test3()) );
>>> which is obviously wrong.
>>>
>>> See:
>>> https://github.com/D-Programming-Language/druntime/blob/master/src/object_.d#L116
>>>
>>
>> The ddoc for the method is wrong. It should say something like:
>>
>> returns != 0 if this object has the same memory address as obj. Subclasses may override this method to provide custom equality comparison.
>>
>> Because for the OP, Test3 has the same contents as the other Test3 (both empty).
>
> "does have the same contents as obj"
>
> yes this is what confused me... and actually, it is a bit wierd coming from a c++ background that obj1 == obj2 is the same as (obj1 is obj2) and not per field comparison as expected (and documented). What is the reasoning behind this... principle of most astonishment? :)
C++ is the odd one out here, I believe. Most OO languages compare addresses by default.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply