Change the Hash Datatype in 3.0?
It has come time to decide if we want to change the hash datatype for REBOL 3.0.
As you know, the hash datatype is implemented as a special type of block that can also be accessed with hash keys. It is quite powerful, but it is a little odd for many users, especially for those who think it should work like the dictionaries found in other languages.
So, what do we want to do about hash? Now is the time to make changes.
Current Behavior
Let's start with a simple set of examples of how hash works:
As you know, you can create a new hash with:
where: make hash! 100
Then, you can add some keys and values with code such as:
append where [bob "Ukiah"]
append where [mary "Eureka"]
Or, you could create the hash directly from the block with:
where: make hash! [bob "Ukiah" mary "Eureka"]
Now, you can use key values to select content values with code such as:
probe select where 'bob
"Ukiah"
probe where/mary
"Eureka"
And, because hash types also act as blocks, you can do block operations too:
1. Pick keys and content values by index position:
probe pick where 3
mary
2. Change keys or content using block functions:
change find where 'mary 'linda
3. Iterate over a hash as a block:
foreach [key value] where [print [key value]]
bob Ukiah
linda Eureka
4. Easily convert back to a block:
probe to-block where
[bob "Ukiah" linda "Eureka"]
5. Serialize (mold) the hash dictionary:
probe mold where
{make hash! [bob "Ukiah" linda "Eureka"]}
And, there are many more... too many to show here.
Those are useful features, don't you think?
Accessing hashes as blocks is a powerful feature.
What's Wrong?
So, what's wrong with REBOL hashes as dictionaries? I can imagine these items could be the main issues people think need to be corrected:
1. All values are keys, even the content values.
Another way to say this is: hashes are not key-value paired (no skip 2 relationship). Everything is hashed.
Here is an example that uses a content value as a key:
probe select where "Ukiah"
linda
This becomes a problem if your keys and your content both use the same datatype (e.g. strings) and can include the same values. For example, if you are using a hash to convert English to French, it would be a problem. (Because French and English share many of the same words, but not the same translations.)
2. You cannot use keys directly as indices.
For example, you cannot write:
pick where 'mary
poke where 'mary "Willits"
That's because pick and poke expect numeric indices to address the block part of the hash type.
3. You cannot add new values using the key value assignment form.
That is, you cannot write:
where/tom: "Willits"
because 'tom is not part of the hash block.
4. You have no control over case-sensitivity for hashes.
select unix-cmds "cd"
select unix-cmds "CD"
These produce the same results.
What to do?
So, what do we want to do about it? We are at a point where we need to finalize the hash datatype for REBOL 3.0. It is possible to make some changes.
Speak now, or forever hold your peace.
52 Comments
|