Thread overview
Why I can't call toHash in const context?
Aug 06, 2013
Borislav Kosharov
Aug 06, 2013
Peter Alexander
Aug 06, 2013
Borislav Kosharov
Aug 06, 2013
Borislav Kosharov
Aug 06, 2013
Peter Alexander
Aug 10, 2013
H. S. Teoh
August 06, 2013
I have this code:

class Animation {
    string name;
}

struct Key {
    Animation from;
    Animation to;

    this(Animation from, Animation to) {
        this.from = from;
        this.to = to;
    }

    const hash_t opHash() {
        return from.toHash() ^ to.toHash();
    }

    const bool opEquals(ref const Key other) {
        return this.from == other.from && this.to == other.to;
    }
}

One class Animation and another Key. And I get this error:
"opHash.d(15): Error: mutable method object.Object.toHash is not callable using a const object"
I need Key to be a struct and I need it to have toHash for associative arrays.

I looked in the docs and it said that I need 'const hash_t opHash' to do that.

Am I missing something?
August 06, 2013
On Tuesday, 6 August 2013 at 18:14:29 UTC, Borislav Kosharov wrote:
> Am I missing something?

It's a long story :-)

Issue 1824 - Object not const correct
http://d.puremagic.com/issues/show_bug.cgi?id=1824

Issue 9771 - Remove toHash from Object
http://d.puremagic.com/issues/show_bug.cgi?id=9771

http://forum.dlang.org/thread/jtlj1k$1fdj$1@digitalmars.com#post-jtlj1k:241fdj:241:40digitalmars.com
August 06, 2013
On Tuesday, 6 August 2013 at 18:18:43 UTC, Peter Alexander wrote:
> On Tuesday, 6 August 2013 at 18:14:29 UTC, Borislav Kosharov wrote:
>> Am I missing something?
>
> It's a long story :-)
>
> Issue 1824 - Object not const correct
> http://d.puremagic.com/issues/show_bug.cgi?id=1824
>
> Issue 9771 - Remove toHash from Object
> http://d.puremagic.com/issues/show_bug.cgi?id=9771
>
> http://forum.dlang.org/thread/jtlj1k$1fdj$1@digitalmars.com#post-jtlj1k:241fdj:241:40digitalmars.com

I don't have much time to read all that, but from what I have skimmed I should remove the const attribute and it should work. And it is considered removing toHash from object?

Thanks for the info. I will read the links later.
August 06, 2013
On Tuesday, 6 August 2013 at 18:18:43 UTC, Peter Alexander wrote:
> On Tuesday, 6 August 2013 at 18:14:29 UTC, Borislav Kosharov wrote:
>> Am I missing something?
>
> It's a long story :-)
>
> Issue 1824 - Object not const correct
> http://d.puremagic.com/issues/show_bug.cgi?id=1824
>
> Issue 9771 - Remove toHash from Object
> http://d.puremagic.com/issues/show_bug.cgi?id=9771
>
> http://forum.dlang.org/thread/jtlj1k$1fdj$1@digitalmars.com#post-jtlj1k:241fdj:241:40digitalmars.com

Also what is the way of calculating hashes for basic types like int and string. Those types don't inherit from Object, right? Is there some std function like hash(T)(T value) that is universal or something?
August 06, 2013
On Tuesday, 6 August 2013 at 18:44:52 UTC, Borislav Kosharov wrote:
> Also what is the way of calculating hashes for basic types like int and string. Those types don't inherit from Object, right?

Correct.

> Is there some std function like hash(T)(T value) that is universal or something?

http://dlang.org/phobos/object.html#.TypeInfo.getHash

Use like so:

T value;
auto hash = typeid(value).getHash(&value);
August 10, 2013
On Tue, Aug 06, 2013 at 08:44:51PM +0200, Borislav Kosharov wrote:
> On Tuesday, 6 August 2013 at 18:18:43 UTC, Peter Alexander wrote:
> >On Tuesday, 6 August 2013 at 18:14:29 UTC, Borislav Kosharov wrote:
> >>Am I missing something?
> >
> >It's a long story :-)
> >
> >Issue 1824 - Object not const correct http://d.puremagic.com/issues/show_bug.cgi?id=1824
> >
> >Issue 9771 - Remove toHash from Object http://d.puremagic.com/issues/show_bug.cgi?id=9771
> >
> >http://forum.dlang.org/thread/jtlj1k$1fdj$1@digitalmars.com#post-jtlj1k:241fdj:241:40digitalmars.com
> 
> Also what is the way of calculating hashes for basic types like int and string. Those types don't inherit from Object, right? Is there some std function like hash(T)(T value) that is universal or something?

Currently, the most reliable way to get hashes for basic types that also matches what AA's use, is to do this:

	int x = 123;	// or, int[] x = [1,2,3], etc.
	auto hash = typeid(x).getHash(&x);

I say "currently" because this is likely to change in the future when we clean up the AA implementation.


T

-- 
War doesn't prove who's right, just who's left. -- BSD Games' Fortune