munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.5k stars 1.01k forks source link

20 Hash tables challenge #1034

Open jdehaan2014 opened 2 years ago

jdehaan2014 commented 2 years ago

In the answers to the challenge to make the hash table accept Value as a Key, I have a question on TableGet. The diff states:

-bool tableGet(Table table, ObjString key, Value value) { +bool tableGet(Table table, Value key, Value* value) { if (table->entries == NULL) return false;

Entry* entry = findEntry(table->entries, table->capacity, key); -if (entry->key == NULL) return false; +if (IS_NIL(entry->key)) return false;

Should that last line not read:

+if (IS_EMPTY(entry->key)) return false;

This because we prefill the table with keys as Empty, and also in the TableDelete, we fill the key as Empty. This means a Key will never be Nil. Or do I make a thinking mistake here?