Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strformat does not work when iterating through object fields/fieldPairs #11046

Open
kaushalmodi opened this issue Apr 16, 2019 · 4 comments

Comments

Projects
None yet
4 participants
@kaushalmodi
Copy link
Contributor

commented Apr 16, 2019

I am unable to use strformat to print the key/val values when looping through an object's fields() or fieldPairs() iterators.

Example

import strformat

type
  Animal = object
    name, species: string

let
  a = Animal(name: "Foo", species: "Bar")

for key, val in a.fieldPairs:
  echo fmt"{key} = {val}"

Current Output

t.nim(11, 11) template/generic instantiation of `fmt` from here
/home/kmodi/usr_local/apps/6/nim/devel/lib/core/macros.nim(539, 1) Error: undeclared identifier: 'key'

Expected Output

I did not expect a compilation failure here.

Additional Information

$ nim -v
Nim Compiler Version 0.19.9 [Linux: amd64]
Compiled at 2019-04-15
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: e2848ccd2bb1da93d729a6dffb201cea80b9cae4
active boot switches: -d:release
@narimiran

This comment has been minimized.

Copy link
Member

commented Apr 17, 2019

This is because of the fieldPairs and it is related to #7533

Workaround:

for key, val in a.fieldPairs:
  let
    k = key
    v = val
  echo fmt"{k} = {v}"
@kaushalmodi

This comment has been minimized.

Copy link
Contributor Author

commented Apr 17, 2019

Thank you. I confirm that the workaround works. But please keep this issue open as I am hoping for this to get fixed.

@kaushalmodi

This comment has been minimized.

Copy link
Contributor Author

commented Apr 17, 2019

@krux02

This comment has been minimized.

Copy link
Contributor

commented Apr 17, 2019

@kaushalmodi posting workarounds is our support. It will allow you to move on. It doesn't mean that the issue is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.