yglukhov / nimpy

Nim - Python bridge
MIT License
1.48k stars 60 forks source link

Nim Tuple with names produce a Python NamedTuple #161

Open juancarlospaco opened 4 years ago

juancarlospaco commented 4 years ago

Nim Tuple with names :arrow_right: Python collections.NamedTuple:

proc foo(u: any): auto {.exportpy.} =
  result = (scheme: u.scheme, username: u.username, password: u.password, hostname: u.hostname,
    port: u.port, path: u.path, query: u.query, anchor: u.anchor, opaque: u.opaque)

Should produce on Python:

(scheme: "value", username: "value", password: "value", hostname: "value",
  port: "value", path: "value", query: "value", anchor: "value", opaque: "value")

Current output is:

("value", "value", "value", "value",
 "value", "value", "value", "value", "value")
yglukhov commented 3 years ago

I don't see how this can be done. Python named tuples are actually named classes. Nim named tuples only have names for their members. What name should we use as a class name for python tuple?

juancarlospaco commented 3 years ago

So looks like it takes _ as name, in a weird "anonymous named" tuple.

import collections
collections.namedtuple("_", "key0 key1")("foo", 42)

Source: https://github.com/nim-lang/Nim/wiki/Nim-for-Python-Programmers#named-tuple

Example: https://tio.run/##K6gsycjPM/7/PzO3IL@oRCE5PycnNbkkMz@vmCtRwRaZr5eXmJuaUlJakJOqoRSvpKOglJ1aaaAAJAyVNDWU0vLzgWImRppcSSTpS0osAoqZmZlpchUUZeaVaCTCGEma//8DAA

Thanks for all the work done, it is really an awesome project!. 🙂:+1:

yglukhov commented 3 years ago

Ok, that could work :)

juancarlospaco commented 2 years ago

Any news on this?, this feature would be awesome. 🙂