www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Using Classes as the KeyType (from the Docs)

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Hi,

There's an example in the docs about classes with a keytype which I don't fully
understand. Here's the code (taken from
http://www.digitalmars.com/d/2.0/arrays.html under "Using Classes as the
KeyType"):

------------------------------------------------------
class Foo
{
    int a, b;

    hash_t toHash() { return a + b; }

    bool opEquals(Object o)
    {	Foo f = cast(Foo) o;
	return f && a == foo.a && b == foo.b;
    }

    int opCmp(Object o)
    {	Foo f = cast(Foo) o;
	if (!f)
	    return -1;
	if (a == foo.a)
	    return b - foo.b;
	return a - foo.a;
    }
}
------------------------------------------------------


Shouldn't this be written like this instead?:
------------------------------------------------------
class Foo
{
    int a, b;

    hash_t toHash() { return a + b; }

    bool opEquals(Object o)
    {	Foo f = cast(Foo) o;
	return f && a == f.a && b == f.b;
    }

    int opCmp(Object o)
    {	Foo f = cast(Foo) o;
	if (!f)
	    return -1;
	if (a == f.a)
	    return b - f.b;
	return a - f.a;
    }
}
------------------------------------------------------

Notice I've replaced "foo" with "f"

Foo is the class, but there's no mention of "foo". I'm guessing it was a typo,
unless I'm missunderstanding something.
Jun 20 2010
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 20 Jun 2010 16:50:04 -0400, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:


 Notice I've replaced "foo" with "f"

 Foo is the class, but there's no mention of "foo". I'm guessing it was a  
 typo, unless I'm missunderstanding something.

Yes, it's a typo. You should file a bug. -Steve
Jun 21 2010
next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Steven Schveighoffer Wrote:

 On Sun, 20 Jun 2010 16:50:04 -0400, Andrej Mitrovic  
 <andrej.mitrovich gmail.com> wrote:
 
 
 Notice I've replaced "foo" with "f"

 Foo is the class, but there's no mention of "foo". I'm guessing it was a  
 typo, unless I'm missunderstanding something.

Yes, it's a typo. You should file a bug. -Steve

Will do. But I think I've noticed some more errors in other places, maybe I should try catching as many of them and then do a single report so they all get fixed in a single run.
Jun 21 2010
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Brad Roberts Wrote:

 On 6/21/2010 9:18 AM, Andrej Mitrovic wrote:
 Steven Schveighoffer Wrote:
 
 On Sun, 20 Jun 2010 16:50:04 -0400, Andrej Mitrovic  
 <andrej.mitrovich gmail.com> wrote:


 Notice I've replaced "foo" with "f"

 Foo is the class, but there's no mention of "foo". I'm guessing it was a  
 typo, unless I'm missunderstanding something.

Yes, it's a typo. You should file a bug. -Steve

Will do. But I think I've noticed some more errors in other places, maybe I should try catching as many of them and then do a single report so they all get fixed in a single run.

More small reports would be better than delayed big reports. Just file as you find 'em.

It shouldn't take long, maybe a few days or so (and there's probably not *that* many errors, but I have noticed some). Anyways I'll start hunting.
Jun 21 2010
prev sibling parent Brad Roberts <braddr puremagic.com> writes:
On 6/21/2010 9:18 AM, Andrej Mitrovic wrote:
 Steven Schveighoffer Wrote:
 
 On Sun, 20 Jun 2010 16:50:04 -0400, Andrej Mitrovic  
 <andrej.mitrovich gmail.com> wrote:


 Notice I've replaced "foo" with "f"

 Foo is the class, but there's no mention of "foo". I'm guessing it was a  
 typo, unless I'm missunderstanding something.

Yes, it's a typo. You should file a bug. -Steve

Will do. But I think I've noticed some more errors in other places, maybe I should try catching as many of them and then do a single report so they all get fixed in a single run.

More small reports would be better than delayed big reports. Just file as you find 'em.
Jun 21 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 21 Jun 2010 12:18:47 -0400, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 Steven Schveighoffer Wrote:

 On Sun, 20 Jun 2010 16:50:04 -0400, Andrej Mitrovic
 <andrej.mitrovich gmail.com> wrote:


 Notice I've replaced "foo" with "f"

 Foo is the class, but there's no mention of "foo". I'm guessing it  

 typo, unless I'm missunderstanding something.

Yes, it's a typo. You should file a bug. -Steve

Will do. But I think I've noticed some more errors in other places, maybe I should try catching as many of them and then do a single report so they all get fixed in a single run.

That would be great thanks! -Steve
Jun 21 2010