[plt-scheme] hash-has-key?

From: Sam TH (samth at ccs.neu.edu)
Date: Mon Mar 30 10:00:18 EDT 2009

On Mon, Mar 30, 2009 at 9:51 AM, Thomas Chust <chust at web.de> wrote:
> 2009-03-30 Sam TH <samth at ccs.neu.edu>:
>> [...]
>> What if the hash table had type Hash[String, Any]?
>
> Then the return value of hash-ref would have type Option[Any], any
> value found in the hash table would be returned by hash-ref wrapped as
> a Some[Any] -- I don't see any problem there.

Consider this program fragment (in some pseudo-syntax):

let h : Hash[String,Any] = (make-hash ...)
let x : Any = (hash-ref h "foo")

Does this code do auto-unwrapping?

>> [...]
>> What if our language contains type inference?  What is the inferred
>> type of (hash-ref h k)?
>
> For any h of type Hash[K, V], hash-ref either extracts a value of type
> V from h and returns it wrapped up as a Some[V] or it returns the
> unique value of type None. The most specific common supertype of
> Some[V] and None is Option[V] and this should be the type inferred for
> hash-ref -- I don't see any problem here, either.

Let's say that I write this modification of the above code:

let h : Hash[String,Number] = (make-hash ...)
let x  = (hash-ref h "foo")

what type is inferred for `x'?

>> You should look at Richard Cobbe's dissertation [1], which contains a
>> fuller discussion of these issues in the context of Java.
>
> I downloaded it and put it on the stack of electronic publications to
> look at. I only gave it a quick glance so far since a bunch of
> articles from my own field of reseach have higher priority to be
> read...
>
> However, I think comparing the none value we are discussing here with
> the null value of Java and many other languages doesn't make much
> sense. The none value used in my examples is a value of a very
> specific type while the null value of Java and others is usually a
> value of (nearly) every type. Therefore the two values have very
> different useful applications.

Really, you should look at the dissertation. Part of it discusses
Option types, and precisely these issues.

-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.