Hey, so I'm trying to now write a VERY simple engine for text adventure games. One of the aspects of it are Exits. These are attached to Door objects and check to see if the way is locked, and if the user is using a particular item on it. I've written a version of this engine in Python (it's the language I have the most experience with), and I was wondering if I could have the keys in the table be of differing types. Here's the code I have working,
type
Item* = object
name: string
description: string
Container* = object
name: string
description: string
contained: seq[Item]
Exit* = object
locked: bool
uses: Table[Object, proc(engine: Engine)]
room: int
So basically, where there is 'Table[Object ...]' in Exit, I would like it to either accept a Container or Item object as a valid key. Would I need to wrap both up as a single object? Or is there a way I could have the table accept either object as valid keys? Thank you for the help! :)For array, seq or table in Nim all elements must have same size and type. Same size because mapping of index to actual element position is done by multiplying size with index.
You may use ref object, as references are basically pointers and so all have same size, or you may use sum types, called object variants in Nim.
What you also could do (since we're talking about the key not the value right?) write a hash proc for each of your keys and let the table use that hash as its key.