Can I get object's field value by variable?
In javascript I can do something like this:
var obj = {
a: 10,
b: 20
}
var c = "a"
console.log(obj[c])
it's possible like this (not tested)
proc `[]`(obj: JsObject, key: string): ??? {.importjs: "#[#]".}
but you don't know the return typeI think the question was how you can access a field in an object through a variable holding the name of the field.
Unfortunately (at least in this case, most often it is very fortunate) Nim is a statically typed compiled language, so the names of fields aren't really accessible at runtime. You could of course achieve similar things through meta-programming, but before reaching into that I think it's wise to look at what you're actually trying to achieve. What is the problem you're trying to solve by doing this? There might be a way to do what you want in a way that's more suitable.