[racket] objects as hash keys

From: Tobias Hammer (tobias.hammer at dlr.de)
Date: Fri Oct 19 12:46:20 EDT 2012

 From the docs (3.13 Hash Tables):
Caveat concerning mutable keys: If a key in an equal?-based hash table is  
mutated (e.g., a key string is modified with string-set!), then the hash  
table’s behavior for insertion and lookup operations becomes unpredictable.

Try make-hasheq instead of make-hash, that should work here.



On Fri, 19 Oct 2012 17:57:41 +0200, Ted <tedr56 at gmail.com> wrote:

> Hi the list,
> I was wondering for my midi/osc -> event mapping project if it was
> possible to put objects instances (with racket/class) as hash keys.
> I tried and it worked. The problem is, I pushed further and when I
> change object fields, the hash can't find my key anymore, despite I see
> it in.
>
> For the record:
>
> (require racket/class)
>
> (define test%
>     (class object%
>         (inspect #f)
>         (init-field
>             address
>         )
>         (define/public (getAddress)
>             address
>         )
>         (define/public (setAddress n)
>             (set! address n)
>         )
>         (super-new)
>     )
> )
>
> (define testy (new test% (address 12)))
> (define testH (make-hash))
> (hash-set! testH testy (send testy getAddress))
>
>> testH
> #hash((#(struct:object:test% 12) . 12))
>
>> (send testy setAddress 13)
>> testH
> #hash((#(struct:object:test% 13) . 12))
>> (hash-has-key testH testy)
> #f
>> (hash-ref testH testy)
> hash-ref: no value found for key: (object:test% 13)
>
> That's an odd response. I know it's an odd request too, but if anyone
> could help me on this, even it I have to use an other solution.
>
> Thanks in advance.
>
> Ted


Posted on the users mailing list.