I don't like that @yglukhov... it may also be not a good solution here, because
having many keys would mean a lot of boilerplate code.
It simply should not be possible normally. But then we are using Nim for a
reason:
# nim js -d:release -d:nodejs --verbosity:0
type Foo = ref object
a: cstring
b: int
proc isUndefined*[T](x: T): bool {.importcpp: "((#)==undefined)".}
proc main() =
var o {.noinit.}: Foo
{.emit: "var `o` = {};".}
o.a = "test"
#[ this is Nim not javascript
if o.b == nil:
echo "o.b is not is nil"
]#
if o.b.isUndefined:
echo "but then we are Nim and Nim is King"
main()
I have some more stuff like a `JSAssoc` object in my Nim-Screeps project which
may be of interest. We used something similar for our PHP backend to interface
to PHPs associative Arrays. But that is "freeform" again. In Nim-Screeps I try
to trick everything into fully typed code. Which pretty much works very well
IMHO.
I also take suggestions of course. I merely hacked that together myself in the
last weeks (with a little help and some small compiler fixes by @araq).
[https://github.com/oderwat/nim-screeps/blob/master/src/jsext.nim](https://github.com/oderwat/nim-screeps/blob/master/src/jsext.nim)