timotheecour / Nim

Nim is a compiled, garbage-collected systems programming language with a design that focuses on efficiency, expressiveness, and elegance (in that order of priority).
http://nim-lang.org/
Other
2 stars 0 forks source link

TableConstr: {"a1": 1, "a2": 2} prints as `[("a1", 1), ("a2", 2)]` #551

Closed timotheecour closed 3 years ago

timotheecour commented 3 years ago

not clear whether it's a bug but a TableConstr litteral IMO could print as {"a1": 1, "a2": 2} instead of [("a1", 1), ("a2", 2)]?

Example

const x = {"a1": 1, "a2": 2}
echo x
echo type(x)

import macros
macro bar() =
  let ret = quote do:
    const x = {"a1": 1, "a2": 2}
  echo ret.repr
  echo ret.treeRepr
bar()

Current Output

const
  x`gensym0 = {"a1": 1, "a2": 2}
ConstSection
  ConstDef
    Ident "x`gensym0"
    Empty
    TableConstr
      ExprColonExpr
        StrLit "a1"
        IntLit 1
      ExprColonExpr
        StrLit "a2"
        IntLit 2

[("a1", 1), ("a2", 2)]
array[0..1, (string, int)]

Current Output

{"a1": 1, "a2": 2} insetead of [("a1", 1), ("a2", 2)] ?

Additional Information

1.5.1 2b5841cd2b096624987d7cd1aa217088447076b7

juancarlospaco commented 3 years ago

AFAIK totes intended, the {"k", "v"} is meant as sugar for [("k", "v")] by design, so not a bug.

timotheecour commented 3 years ago

i know, but this violates the fact that repr looks differs from $ whereas it often is the same; (very vaguely related to homoiconicity) ideally an AST would stringify to itself; this could be fixable in $ by distinguishing on whether we pass in a litteral, using this or similar

proc `$`{nnkTableConstr}[T](a: T): string

not a big deal though, closing

juancarlospaco commented 3 years ago

repr does look different from $.

The reality is often the $ is more popular on usage, then more fixes, then looks better.