Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 03, 2010 Confused about class equality | ||||
---|---|---|---|---|
| ||||
The program below outputs, as I would expect : Same Value. Same Object. 3 : 44E15C 0000 3 : 44E15C 0000 5 : 44E15C 0000 5 : 44E15C 0000 Now what would it mean if it were to output : Same Value. 3 : 5B536C 59D020 3 : 59CE0C 59CEF0 5 : 5B536C 59D020 5 : 59CE0C 59CEF0 (Output from essentially the same piece of code within a larger project) Not the same object, but still able to change it with one call. What is it I don't get? ------ module main; import std.stdio; import std.string; interface I{ char[] toString(); int value(); void value(int v); } class C : I{ private int _value; this(int v){ _value = v; } char[] toString(){ return format(_value); }; int value(){ return _value; }; void value(int v){ _value = v; }; } private I[3][3] arr; public I getI( int x, int y){ return arr[x][y]; } public void setI( int x, int y, I i ){ arr[x][y]= i; } void main(){ C c = new C(3); setI( 0, 0, c ); setI( 1, 2, c ); I i1 = null; I i2 = null; i1 = getI(0,0); i2 = getI(1,2); if( i1 !is null && i2 !is null && i2.value == i1.value ) { writefln("Same Value."); if( i2 is i1 ) writefln("Same Object."); writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor); writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor); i1.value = 5; writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor); writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor); } } |
April 03, 2010 Re: Confused about class equality | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On Fri, 02 Apr 2010 22:36:40 -0400, strtr <strtr@spam.com> wrote:
>
> The program below outputs, as I would expect :
> Same Value.
> Same Object.
> 3 : 44E15C 0000
> 3 : 44E15C 0000
> 5 : 44E15C 0000
> 5 : 44E15C 0000
>
> Now what would it mean if it were to output :
> Same Value.
> 3 : 5B536C 59D020
> 3 : 59CE0C 59CEF0
> 5 : 5B536C 59D020
> 5 : 59CE0C 59CEF0
> (Output from essentially the same piece of code within a larger project)
>
> Not the same object, but still able to change it with one call. What is it I don't get?
>
> ------
> module main;
>
> import std.stdio;
> import std.string;
>
> interface I{
> char[] toString();
> int value();
> void value(int v);
> }
>
> class C : I{
> private int _value;
>
> this(int v){ _value = v; }
> char[] toString(){ return format(_value); };
> int value(){ return _value; };
> void value(int v){ _value = v; };
> }
> private I[3][3] arr;
>
> public I getI( int x, int y){ return arr[x][y]; }
> public void setI( int x, int y, I i ){ arr[x][y]= i; }
>
> void main(){
> C c = new C(3);
> setI( 0, 0, c );
> setI( 1, 2, c );
> I i1 = null;
You may want to try reducing the code within your "larger project" to the smallest use case where the problem still occurs, because, I agree, the code as written will always result in the same object, since you allocate only one.
|
April 03, 2010 Re: Confused about class equality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Spahr-Summers | Justin Spahr-Summers Wrote:
> On Fri, 02 Apr 2010 22:36:40 -0400, strtr <strtr@spam.com> wrote:
>
> You may want to try reducing the code within your "larger project" to the smallest use case where the problem still occurs, because, I agree, the code as written will always result in the same object, since you allocate only one.
The main starting from "I i1 = null;" is actual code from my program. The getI function is also the same and no code resides in between, nor is it multi-threaded. Within this setting, how can you manipulate the objects in arr(which is larger in the actual code) to get the strange output?
|
April 03, 2010 Re: Confused about class equality | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | What I probably mean to ask is : In the code below, for what kind of i1 and i2 would the output be like this : --------- Same Value. 3 : 5B536C 59D020 3 : 59CE0C 59CEF0 5 : 5B536C 59D020 5 : 59CE0C 59CEF0 --------- if( i1 !is null && i2 !is null && i2.value == i1.value ) { writefln("Same Value."); if( i2 is i1 ) writefln("Same Object."); writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor); writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor); i1.value = 5; writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor); writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor); } |
April 03, 2010 Re: Confused about class equality | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On 4/3/10 07:03, strtr wrote:
> What I probably mean to ask is :
> In the code below, for what kind of i1 and i2 would the output be like this :
> ---------
> Same Value.
> 3 : 5B536C 59D020
> 3 : 59CE0C 59CEF0
> 5 : 5B536C 59D020
> 5 : 59CE0C 59CEF0
> ---------
> if( i1 !is null&& i2 !is null&& i2.value == i1.value ) {
> writefln("Same Value.");
> if( i2 is i1 ) writefln("Same Object.");
> writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor);
> writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor);
> i1.value = 5;
> writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor);
> writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor);
> }
>
>
I would guess:
i1 = new I;
i2 = new I;
They are two different instance of "I" but containing the same value. When comparing two objects using "is", the addresses are compared and not the data in the object, i.e. instance variables.
|
April 03, 2010 Re: Confused about class equality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | Jacob Carlborg Wrote:
> On 4/3/10 07:03, strtr wrote:
> > What I probably mean to ask is :
> > In the code below, for what kind of i1 and i2 would the output be like this :
> > ---------
> > Same Value.
> > 3 : 5B536C 59D020
> > 3 : 59CE0C 59CEF0
> > 5 : 5B536C 59D020
> > 5 : 59CE0C 59CEF0
> > ---------
> > if( i1 !is null&& i2 !is null&& i2.value == i1.value ) {
> > writefln("Same Value.");
> > if( i2 is i1 ) writefln("Same Object.");
> > writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor);
> > writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor);
> > i1.value = 5;
> > writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor);
> > writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor);
> > }
> >
> >
>
> I would guess:
>
> i1 = new I;
> i2 = new I;
>
> They are two different instance of "I" but containing the same value. When comparing two objects using "is", the addresses are compared and not the data in the object, i.e. instance variables.
Wouldn't that simply result in the following?
3 : 5B536C 59D020
3 : 59CE0C 59CEF0
5 : 5B536C 59D020
3 : 59CE0C 59CEF0
|
April 03, 2010 Re: Confused about class equality | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | strtr wrote:
> The program below outputs, as I would expect :
> Same Value.
> Same Object.
> 3 : 44E15C 0000
> 3 : 44E15C 0000
> 5 : 44E15C 0000
> 5 : 44E15C 0000
>
> Now what would it mean if it were to output :
> Same Value.
> 3 : 5B536C 59D020
> 3 : 59CE0C 59CEF0
> 5 : 5B536C 59D020
> 5 : 59CE0C 59CEF0
> (Output from essentially the same piece of code within a larger project)
>
> Not the same object, but still able to change it with one call.
> What is it I don't get?
>
> ------
> module main;
>
> import std.stdio;
> import std.string;
>
> interface I{
> char[] toString();
> int value();
> void value(int v);
> }
>
> class C : I{
> private int _value;
>
> this(int v){ _value = v; }
> char[] toString(){ return format(_value); };
> int value(){ return _value; };
> void value(int v){ _value = v; };
> }
> private I[3][3] arr;
>
> public I getI( int x, int y){ return arr[x][y]; }
> public void setI( int x, int y, I i ){ arr[x][y]= i; }
>
> void main(){
> C c = new C(3);
> setI( 0, 0, c );
> setI( 1, 2, c );
> I i1 = null;
> I i2 = null;
> i1 = getI(0,0);
> i2 = getI(1,2);
>
> if( i1 !is null && i2 !is null && i2.value == i1.value ) {
> writefln("Same Value.");
> if( i2 is i1 ) writefln("Same Object.");
> writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor);
> writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor);
> i1.value = 5;
> writefln( i2.toString()," : ",i2.__vptr," ",i2.__monitor);
> writefln( i1.toString(), " : ", i1.__vptr," ",i1.__monitor);
> }
> }
The code works as expected with 2.042
I had to modify the toString() functions to return string, and say "override" in C's toString definition; and had to modify the writefln() calls:
writefln("%s : %s %s", i2.toString(), i2.__vptr, i2.__monitor);
The output:
Same Value.
Same Object.
3 : 806D3F4 0
3 : 806D3F4 0
5 : 806D3F4 0
5 : 806D3F4 0
Ali
|
April 04, 2010 Re: Confused about class equality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Ali Çehreli Wrote:
>
> The code works as expected with 2.042
>
> I had to modify the toString() functions to return string, and say "override" in C's toString definition; and had to modify the writefln() calls:
>
> writefln("%s : %s %s", i2.toString(), i2.__vptr, i2.__monitor);
>
> The output:
>
> Same Value.
> Same Object.
> 3 : 806D3F4 0
> 3 : 806D3F4 0
> 5 : 806D3F4 0
> 5 : 806D3F4 0
>
> Ali
I probably wasn't clear about what exactly my problem is :
Somehow in my (D1) program two object references have different vpointers/monitors and thus fail in "is" equality but I can change both objects with one call.
|
April 04, 2010 Re: Confused about class equality | ||||
---|---|---|---|---|
| ||||
Posted in reply to strtr | On Sun, 04 Apr 2010 13:14:29 -0400, strtr <strtr@spam.com> wrote:
>
> Ali Çehreli Wrote:
> >
> > The code works as expected with 2.042
> >
> > I had to modify the toString() functions to return string, and say "override" in C's toString definition; and had to modify the writefln() calls:
> >
> > writefln("%s : %s %s", i2.toString(), i2.__vptr, i2.__monitor);
> >
> > The output:
> >
> > Same Value.
> > Same Object.
> > 3 : 806D3F4 0
> > 3 : 806D3F4 0
> > 5 : 806D3F4 0
> > 5 : 806D3F4 0
> >
> > Ali
>
> I probably wasn't clear about what exactly my problem is :
> Somehow in my (D1) program two object references have different vpointers/monitors and thus fail in "is" equality but I can change both objects with one call.
Again, without seeing the *actual* code that you are using, it's hard to say. The first thing that comes to mind is maybe a syntactical mistake, such as "i1.value = i2.value" when a comparison was intended, but there could be something else at play too, and without seeing the code (which is not the same as what you included in your first message) it's impossible to ascertain.
|
April 04, 2010 Re: Confused about class equality | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Spahr-Summers | == Quote from Justin Spahr-Summers (Justin.SpahrSummers@gmail.com)'s article > Again, without seeing the *actual* code that you are using, it's hard to say. The first thing that comes to mind is maybe a syntactical mistake, such as "i1.value = i2.value" when a comparison was intended, but there could be something else at play too, and without seeing the code (which is not the same as what you included in your first message) it's impossible to ascertain. But also, the question remains :) (I haven't been able to replicate it in small program but the main is actual code.) Is it possible to have different vpointers/monitors pointing to the same object? |
Copyright © 1999-2021 by the D Language Foundation