digitalmars.D - Why I can't call toHash in const context?
- Borislav Kosharov (26/26) Aug 06 2013 I have this code:
- Peter Alexander (8/9) Aug 06 2013 It's a long story :-)
- Borislav Kosharov (5/14) Aug 06 2013 I don't have much time to read all that, but from what I have
- Borislav Kosharov (5/14) Aug 06 2013 Also what is the way of calculating hashes for basic types like
- Peter Alexander (7/11) Aug 06 2013 Correct.
- H. S. Teoh (10/29) Aug 06 2013 Currently, the most reliable way to get hashes for basic types that also
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?
Aug 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
Aug 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: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.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
Aug 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: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?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
Aug 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?Use like so: T value; auto hash = typeid(value).getHash(&value);
Aug 06 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: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' FortuneOn Tuesday, 6 August 2013 at 18:14:29 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? Is there some std function like hash(T)(T value) that is universal or something?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
Aug 06 2013