digitalmars.D - Should I be able to do "is" with pointers?
- Lionello Lunesu (19/19) May 23 2005 Please consider this code:
- Ben Hinkle (10/28) May 23 2005 Making one of the two a compiler error would be odd. Having two operator...
- Lionello Lunesu (4/7) May 23 2005 I guess you're right. It's funny though that this code is the same:
- Ben Hinkle (9/16) May 23 2005 I'm not sure what you mean by same. For example
- Lionello Lunesu (12/23) May 23 2005 Now I'm confused. The last two lines... Your code does indeed work, but ...
- Ben Hinkle (11/34) May 23 2005 Remember objects are manipulated by reference. So in my code value and
- MicroWizard (16/54) May 23 2005 I hope I understand well you both.
- Lionello Lunesu (5/5) May 24 2005 Aha, so
- xs0 (4/10) May 24 2005 The class reference is already a pointer to an object. So,
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
"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
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
"Lionello Lunesu" <lio lunesu.removethis.com> wrote in message news:d6svda$1gm3$1 digitaldaemon.com...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 );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
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.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 );
May 23 2005
"Lionello Lunesu" <lio lunesu.removethis.com> wrote in message news:d6t247$1jnu$1 digitaldaemon.com...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.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...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 );
May 23 2005
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...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.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...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 );
May 23 2005
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
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