[plt-scheme] hash-has-key?

From: YC (yinso.chen at gmail.com)
Date: Sat Mar 28 23:24:13 EDT 2009

On Sat, Mar 28, 2009 at 9:15 AM, Sam TH <samth at ccs.neu.edu> wrote:

>
> This does not help at all.  Imagine that the 'not-found-value' is
> called QQQ.  Then we have two choices:
>
> - QQQ is a value in our language.  Then it can be stored in the hash,
> and we can't tell if we need to throw an exception.  This is the
> current situation.
>

I believe the internal hash-has-key? should be exposed, since hash-ref
obviously has to make such a test in order to decide whether to return the
default value.

But it's possible to implement the QQQ value if the client has to implement
the hash-has-key? function, by utilizing make-struct-type, since each call
to make-struct-type will create a unique struct type that cannot equal any
other structs.  And as long as we do not expose its make function, the
client can never make and store such a value in the hash.

(define hash-has-key?
  (let-values (((s make isa? ref set!)
                (make-struct-type 'QQQ #f 0 0 )))
    (lambda (hash key)
      (not (isa? (hash-ref hash key (make)))))))

Cheers,
yc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090328/5d3c602d/attachment.html>

Posted on the users mailing list.