nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.52k stars 1.46k forks source link

Limit JsAssoc to cstring and int #8601

Closed alehander92 closed 5 years ago

alehander92 commented 6 years ago

JsAssoc can accept various K types for a key, which is something that I happily used. However this usually leads to subtle bugs: JS converts all keys to strings, so

1 conversion to string might be not always unique for some types(e.g. objects) 2 when you iterate, the key is actually a cstring, which means the Nim type system leads you to a probable error in this case

e.g. if you have int, int

var b = JsAssoc[int, cstring]{4: cstring"e"}
for a, _ in b:
  # a

a can easily seem like an int, but it's a cstring, a lot of operations on it will work in the same way even if it's cstring, but sooner or later this leads to a weird bug

Basically we need to limit K to types that are easily <=> with cstring, like int and make special pairs for those types, so we automatically insert a conversion to K. (the V value type should remain the same)

Thoughts? Should I make a PR?

Araq commented 6 years ago

Thoughts? Should I make a PR?

Please!

alehander92 commented 5 years ago

this was also merged