[Nickle] Nickle has hashes, did you know?

Keith Packard keithp at keithp.com
Sun Apr 11 00:50:37 PDT 2004


Bart finally asked me to get hashes (associative arrays) working in nickle,
and I remembered vaguely doing some work in that area some time ago.  Well,
to my surprise, they were actually pretty much completely implemented last
summer.  Gotta be the easiest implementation I've done in a while.

The basic structure is a mapping from value -> value, each of these are 
typechecked.  You declare (and use) a hash using a notation similar to
arrays:

> int[string] token_count;
> token_count["hello"] = 1;
> token_count
(int [string]) { "hello" => 1 }
> token_count["hello"]++;
> token_count["hello"]
2

Bart asked me to add 'default' values.  Because nickle generally assigns 
'uninit' to any uninitialized variable, it's a pain to use hashes in a 
simple fashion like one would with perl, awk or python.   But, there isn't 
any one value which makes sense for all hashes to be set to, so instead we
have a user-specified default value, declared in the hash initializer:

> int[string] token_count = { => 0 };

Use an as-yet-undefined hash entry in any context and one will
automagically be created and initialized to a copy of the default value.

We still have questions about how deletion should work in the face of 
reference values, but those are similar enough to array resizing that 
we'll solve that problem first then tackle the semantically more tricky 
hash deletion.

-keith


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
Url : /pipermail/nickle/attachments/20040411/0938a3ca/attachment.pgp


More information about the Nickle mailing list