www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Should I be able to do "is" with pointers?

reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
Please consider this code:

MyClass value = new MyClass()
MyClass *ptr = &value;
assert( ptr == &value );
assert( ptr is &value );

Apparently, the "is" in this code behaves the same as the "==", which is 
understandable. But should we really be able to write this code (comparing 
pointers) in both these ways?

The problem is that I really wouldn't know which way should be recommended. 
The "==" makes sense, since we're comparing the values of both arguments. 
The "is" makes sense too, since we're also checking the identity for an 
object. Furthermore, the "==" feels like a call to opEquals, but there's no 
such call involved.

Clearly mixing the the two ways is confusing. This confusion arrises from 
the fact that two different coding styles are used, which usually won't be 
the case, or at least shouldn't be the case.

The ugliness of the code set aside, which would you prefer in this case, == 
or "is"?

L. 
May 23 2005
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Lionello Lunesu" <lio lunesu.removethis.com> wrote in message 
news:d6s66j$o76$1 digitaldaemon.com...
 Please consider this code:

 MyClass value = new MyClass()
 MyClass *ptr = &value;
 assert( ptr == &value );
 assert( ptr is &value );

 Apparently, the "is" in this code behaves the same as the "==", which is 
 understandable. But should we really be able to write this code (comparing 
 pointers) in both these ways?
Making one of the two a compiler error would be odd. Having two operators do the same thing for a particular situation is ok with me.
 The problem is that I really wouldn't know which way should be 
 recommended. The "==" makes sense, since we're comparing the values of 
 both arguments. The "is" makes sense too, since we're also checking the 
 identity for an object. Furthermore, the "==" feels like a call to 
 opEquals, but there's no such call involved.

 Clearly mixing the the two ways is confusing. This confusion arrises from 
 the fact that two different coding styles are used, which usually won't be 
 the case, or at least shouldn't be the case.
Is it really that confusing? To me it's almost like how many spaces do you put around identifiers or where you put {}. Switching between the two might look a bit odd but I wouldn't worry about it, personally.
 The ugliness of the code set aside, which would you prefer in this case, 
 == or "is"?
Slight preference for the 'is' because it matches the use for object references. But if I'm porting code from something like C/C++ that uses == I'll probably not bother switching those to 'is'.
May 23 2005
parent reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
 Is it really that confusing? To me it's almost like how many spaces do you 
 put around identifiers or where you put {}. Switching between the two 
 might look a bit odd but I wouldn't worry about it, personally.
I guess you're right. It's funny though that this code is the same: assert( ptr is &value ); assert( *ptr is value ); L.
May 23 2005
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Lionello Lunesu" <lio lunesu.removethis.com> wrote in message 
news:d6svda$1gm3$1 digitaldaemon.com...
 Is it really that confusing? To me it's almost like how many spaces do 
 you put around identifiers or where you put {}. Switching between the two 
 might look a bit odd but I wouldn't worry about it, personally.
I guess you're right. It's funny though that this code is the same: assert( ptr is &value ); assert( *ptr is value ); L.
I'm not sure what you mean by same. For example MyClass value = new MyClass(); MyClass value2 = value; MyClass *ptr = &value2; assert( ptr is &value2 ); assert( !(ptr is &value) ); assert( *ptr is value );
May 23 2005
parent reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
 I guess you're right. It's funny though that this code is the same:

 assert( ptr is &value );
 assert( *ptr is value );
I'm not sure what you mean by same. For example MyClass value = new MyClass(); MyClass value2 = value; MyClass *ptr = &value2; assert( ptr is &value2 ); assert( !(ptr is &value) ); assert( *ptr is value );
Now I'm confused. The last two lines... Your code does indeed work, but mine too: MyClass value = new MyClass(); // constructor MyClass* ptr = &value; // assert(value == *ptr); // opEquals assert(value is *ptr); assert(&value == ptr); assert(&value is ptr); ??? What's going on.. Why are in my program both "ptr is &value" and "*ptr is value" true, and in your's only "*ptr is value"!?!? I must be missing something... L.
May 23 2005
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"Lionello Lunesu" <lio lunesu.removethis.com> wrote in message 
news:d6t247$1jnu$1 digitaldaemon.com...
 I guess you're right. It's funny though that this code is the same:

 assert( ptr is &value );
 assert( *ptr is value );
I'm not sure what you mean by same. For example MyClass value = new MyClass(); MyClass value2 = value; MyClass *ptr = &value2; assert( ptr is &value2 ); assert( !(ptr is &value) ); assert( *ptr is value );
Now I'm confused. The last two lines... Your code does indeed work, but mine too: MyClass value = new MyClass(); // constructor MyClass* ptr = &value; // assert(value == *ptr); // opEquals assert(value is *ptr); assert(&value == ptr); assert(&value is ptr); ??? What's going on.. Why are in my program both "ptr is &value" and "*ptr is value" true, and in your's only "*ptr is value"!?!? I must be missing something...
Remember objects are manipulated by reference. So in my code value and value2 store the same reference but in different memory locations. The assertion assert( ptr is &value ) says that ptr points to the memory location of variable "value" while assert( *ptr is value ); says that the reference pointed to by ptr is the same reference stored in the variable "value". My code was to illustrate you can store the same reference in multiple variables.
May 23 2005
parent reply MicroWizard <MicroWizard_member pathlink.com> writes:
I hope I understand well you both.

Lionello,
I think you mix up the meaning of references and pointers.

In C and C++ a pointer store an address of a thing.
In D a reference is a handle, which refers to a storage
where the real pointer (and other things) are stored.
So the real thing can be moved around in the memory
and only the real pointer should be changed, not all
the references referring that.
That makes possible Garbage Collection to work.

In D it is possible to get the address of a thing,
but I'm almost sure, it is not a reference, it is the real pointer.
So comparing references and pointers does not make any sense.

Tamas Nagy
E-Mail: tamas at microwizard dot hu

In article <d6t4mn$1m87$1 digitaldaemon.com>, Ben Hinkle says...
"Lionello Lunesu" <lio lunesu.removethis.com> wrote in message 
news:d6t247$1jnu$1 digitaldaemon.com...
 I guess you're right. It's funny though that this code is the same:

 assert( ptr is &value );
 assert( *ptr is value );
I'm not sure what you mean by same. For example MyClass value = new MyClass(); MyClass value2 = value; MyClass *ptr = &value2; assert( ptr is &value2 ); assert( !(ptr is &value) ); assert( *ptr is value );
Now I'm confused. The last two lines... Your code does indeed work, but mine too: MyClass value = new MyClass(); // constructor MyClass* ptr = &value; // assert(value == *ptr); // opEquals assert(value is *ptr); assert(&value == ptr); assert(&value is ptr); ??? What's going on.. Why are in my program both "ptr is &value" and "*ptr is value" true, and in your's only "*ptr is value"!?!? I must be missing something...
Remember objects are manipulated by reference. So in my code value and value2 store the same reference but in different memory locations. The assertion assert( ptr is &value ) says that ptr points to the memory location of variable "value" while assert( *ptr is value ); says that the reference pointed to by ptr is the same reference stored in the variable "value". My code was to illustrate you can store the same reference in multiple variables.
May 23 2005
parent reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
Aha, so

MyClass *ptr = &value;

is not just a pointer to the object, but a pointer to a reference?
If so, how do I get a pointer to an object?

L. 
May 24 2005
parent xs0 <xs0 xs0.com> writes:
Lionello Lunesu wrote:
 Aha, so
 
 MyClass *ptr = &value;
 
 is not just a pointer to the object, but a pointer to a reference?
 If so, how do I get a pointer to an object?
The class reference is already a pointer to an object. So, MyClass ptr = value; xs0
May 24 2005