www.digitalmars.com         C & C++   DMDScript  

D - some (Object related) questions

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
Why does class Object have opEquals?

Inside this function it just checks this===o;
This can cause problems! For example
i am writing a class and accidentally forget
to write opEquals or misspell it (opEguals or something)
Then i try to compare two objects, i write a==b
and since i didn't write opEquals the one from Object
is used and my objects are in fact compared for
identity. If i wanted to do that i would have written
a===b or a is b, but it isn't what i wanted!

Is this a potential cause of bugs or are there good
reasons for Object to have opEquals and opCmp(this one
does adress-comparing and if i wanted to do this i
would write &a==&b)

A related question: How can i make demands on my template
type? How do i require a template type to have
opEquals defined (a real one and not just the one from
Object)?
Is there a way to express general requirements like having
opAdd defined..?

Please forgive me for asking so many (maybe stupid)
questions! :)
Apr 12 2004
next sibling parent reply Vathix <vathix dprogramming.com> writes:
Ivan Senji wrote:
 Why does class Object have opEquals?
 
 Inside this function it just checks this===o;
 This can cause problems! For example
 i am writing a class and accidentally forget
 to write opEquals or misspell it (opEguals or something)
 Then i try to compare two objects, i write a==b
 and since i didn't write opEquals the one from Object
 is used and my objects are in fact compared for
 identity. If i wanted to do that i would have written
 a===b or a is b, but it isn't what i wanted!
opEquals exists because it's often faster to compare for equality. Use the override keyword to make sure you're overriding a function.
 Is this a potential cause of bugs or are there good
 reasons for Object to have opEquals and opCmp(this one
 does adress-comparing and if i wanted to do this i
 would write &a==&b)
It's just a basic implementation (for a basic Object), that's why you're supposed to override them to do other things based on your class, such as comparing a member string.
 A related question: How can i make demands on my template
 type? How do i require a template type to have
 opEquals defined (a real one and not just the one from
 Object)?
 Is there a way to express general requirements like having
 opAdd defined..?
An interface can do this. Look at the bottom of http://www.digitalmars.com/d/class.html#interface -- "A reimplemented interface must implement all the interface functions, it does not inherit them from a super class".
 Please forgive me for asking so many (maybe stupid)
 questions! :)
 
 
-- Christopher E. Miller
Apr 12 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Vathix" <vathix dprogramming.com> wrote in message
news:c5f63k$18na$1 digitaldaemon.com...
 Ivan Senji wrote:
 Why does class Object have opEquals?

 Inside this function it just checks this===o;
 This can cause problems! For example
 i am writing a class and accidentally forget
 to write opEquals or misspell it (opEguals or something)
 Then i try to compare two objects, i write a==b
 and since i didn't write opEquals the one from Object
 is used and my objects are in fact compared for
 identity. If i wanted to do that i would have written
 a===b or a is b, but it isn't what i wanted!
opEquals exists because it's often faster to compare for equality. Use the override keyword to make sure you're overriding a function.
 Is this a potential cause of bugs or are there good
 reasons for Object to have opEquals and opCmp(this one
 does adress-comparing and if i wanted to do this i
 would write &a==&b)
It's just a basic implementation (for a basic Object), that's why you're supposed to override them to do other things based on your class, such as comparing a member string.
But why is this a basic implementation? Is it really necessary? Why is opEquals defined like identity? For this we have identity operator === or is. Why is opCmp defined like comparing adresses? This can be done by actually comparing adresses.
 A related question: How can i make demands on my template
 type? How do i require a template type to have
 opEquals defined (a real one and not just the one from
 Object)?
 Is there a way to express general requirements like having
 opAdd defined..?
An interface can do this. Look at the bottom of http://www.digitalmars.com/d/class.html#interface -- "A reimplemented interface must implement all the interface functions, it does not inherit them from a super class".
OK thanks this helps :)
 Please forgive me for asking so many (maybe stupid)
 questions! :)
-- Christopher E. Miller
Apr 12 2004
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
Ivan Senji wrote:
 Why does class Object have opEquals?
I wish it didn't. It's because of this that Object a, b; if (a == b) is perfectly legal code that will cause an access violation every time it is executed. (a is null, so a.opEquals cannot be called withoun violating the invariant that says 'this' must not be null)
 Inside this function it just checks this===o;
 This can cause problems! For example
 i am writing a class and accidentally forget
 to write opEquals or misspell it (opEguals or something)
I always use the override keyword when overriding things. (it causes a compiler error if the method does not override something in the parent)
 A related question: How can i make demands on my template
 type? How do i require a template type to have
 opEquals defined (a real one and not just the one from
 Object)?
 Is there a way to express general requirements like having
 opAdd defined..?
In more mundane situations, you can fake it with something like this: class Foo(T) { // you don't ever have to actually call this. private static bool hasEquals() { return new T() == new T(); } } In the case of Object.opEquals, though, I don't know.
 Please forgive me for asking so many (maybe stupid)
 questions! :)
psh. Far, far dumber questions have been asked. Show us your worst! :) -- andy
Apr 12 2004
next sibling parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Andy Friesen" <andy ikagames.com> wrote in message
news:c5g2bh$2kmf$1 digitaldaemon.com...
 Ivan Senji wrote:
 Why does class Object have opEquals?
I wish it didn't. It's because of this that Object a, b; if (a == b) is perfectly legal code that will cause an access violation every time it is executed. (a is null, so a.opEquals cannot be called withoun violating the invariant that says 'this' must not be null)
So is there anything that we would lose if opEquals an opCmp weren't defined in Object. The way i see it it would elliminate some problems like the one you mentioned. Plus: this definition of opEquals checks for identity and we allready have a good (better!) way for testing this!
 psh.  Far, far dumber questions have been asked.  Show us your worst! :)

   -- andy
Thank for the understanding. Recently i don't have much time to write a lot of D code so i have to do a lot of D-thinking, and this can cause stupid question, when you have something in your head and don't have the time to try it! :)
Apr 13 2004
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Ivan Senji wrote:
 So is there anything that we would lose if opEquals an opCmp weren't defined
 in
 Object. The way i see it it would elliminate some problems like the one you
 mentioned. Plus: this definition of opEquals checks for identity and we
 allready
 have a good (better!) way for testing this!
Yeah, it would mean that we couldn't write overrides in children.
psh.  Far, far dumber questions have been asked.  Show us your worst! :)

  -- andy
Thank for the understanding. Recently i don't have much time to write a lot of D code so i have to do a lot of D-thinking, and this can cause stupid question, when you have something in your head and don't have the time to try it! :)
Apr 13 2004
prev sibling parent Ben Hinkle <bhinkle4 juno.com> writes:
Andy Friesen wrote:
 Ivan Senji wrote:
 
 Why does class Object have opEquals?
I wish it didn't. It's because of this that Object a, b; if (a == b) is perfectly legal code that will cause an access violation every time it is executed. (a is null, so a.opEquals cannot be called withoun violating the invariant that says 'this' must not be null)
There is a little "compiler helper" in src/phobos/internal/obj.d called _d_obj_eq that does the right thing w.r.t. null. I can't tell if it is ever used, though. When I call it directly instead of == it works fine. I don't know if that helper was just not finished or if it was abandoned. -Ben ps. for the curious _d_obj_eq is just return o1 === o2 || (o1 && o1.opEquals(o2));
Apr 13 2004