Table constructor
A table constructor is syntactic sugar for an array constructor:
{"key1": "value1", "key2", "key3": "value2"}
# is the same as:
[("key1", "value1"), ("key2", "value2"), ("key3", "value2")]
The empty table can be written {:} (in contrast to the empty set which is {}) which is thus another way to write the empty array constructor []. This slightly unusual way of supporting tables has lots of advantages:
- The order of the (key,value)-pairs is preserved, thus it is easy to support ordered dicts with for example {key: val}.newOrderedTable.
- A table literal can be put into a const section and the compiler can easily put it into the executable’s data section just like it can for arrays and the generated data section requires a minimal amount of memory.
- Every table implementation is treated equally syntactically.
- Apart from the minimal syntactic sugar, the language core does not need to know about tables.
当前内容版权归 nim-lang.org 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 nim-lang.org .