Hello,
I want to perform fuzzy matching on table keys. What I mean by that, is:
import tables
type
Fn = proc (args: varargs[string]): int
FnSignature = tuple
name: string
arity: int
argTypes: seq[string]
var signatures = newTable[FnSignature, Fn]()
signatures.add(("print", 1, @["any"])) do (args: varargs[string]) -> int:
discard
echo signatures[("print", 1, @["string"])] # should return the first added element, because ``any`` describes any type
From what I know about hash tables, it's not as simple as adding an equality operator overload to FnSignature. How do I achieve what I'm talking about?
Add hash function and equality operator == for custom key.
import tables, hashes
type
Fn = proc (args: varargs[string]): int
FnSignature = tuple
name: string
arity: int
argTypes: seq[string]
proc hash(fns: FnSignature): Hash = fns[0].hash
proc `==`(a, b: FnSignature): bool = a[0] == b[0]
var signatures = newTable[FnSignature, Fn]()
signatures.add(("print", 1, @["any"])) do (args: varargs[string]) -> int:
discard
echo signatures[("print", 1, @["string"])]("whatev")
Well, it sort of did. When implementing the hash method however, I realized it's not that simple, and decided to use something like this:
import tables
type
FnSignatureTable = ref object
signatures: TableRef[string, seq[FnSignature]]
Then I store all signatures in the table with their names as keys, and use an index operator with a signature as the key. I then compare the inputted signature with all the others in the seq, to see if any one matches.