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

String interpolation (eg fmt) raises undeclared identifier but identifier is in scope #9544

Closed
joshgoebel opened this issue Oct 28, 2018 · 2 comments

Comments

@joshgoebel
Copy link
Contributor

joshgoebel commented Oct 28, 2018

I have a macro generating code:

  result.add quote do:
    proc find*(t: type[`model`], id: string | int) : `model` =
      echo tableName # no error here
      ...
      var query = sql(&"select * from {tableName} where {pk}  = ?") # Error: undeclared identifier: 'tableName'

    proc insert*(p: var `model`) : int64 =
      echo "TABLENAME ",&"{tableName}" # no error here
      var query = sql(&"insert into {tableName} {full_insert} values {full_values}") # no error here

tableName is a const at the top-level that has been generated elsewhere.

What happens

What happens is the interpolation in find fails with Error: undeclared identifier: 'tableName'. The interpolation in insert works just fine.

What I expect to happen

What I expect to happen is that the value of tableName is inserted into the string for find (just as it is successfully is for insert.

Possible Solution

If you first assign the constant to anther local variable (jumping thru all the ident quoting hoops) and then try to interpolate it it will work.

IE:

  var tn = ident"tn"
  var idtableName = ident"tableName"
  result.add quote do:
    proc find*(t: type[`model`], id: string | int) : `model` =
      var `tn` = `idtableName`
      var query = sql(&&"select * from {tn} where {pk}  = ?") # Error: undeclared identifier: 'tableName'

This makes working with interpolation in macros crazy cumbersome though. I'm hoping I'm mising something.

Additional Information

I found and tried -d:debugFmtDsl but it didn't reveal anything I thought would be useful.

@krux02
Copy link
Contributor

krux02 commented Oct 28, 2018

Can you extend the source code so that I can reproduce the error?

EDIT:

I just read your example again. String interpolation doesn't work in macro/template context. This is a known problem. Use string concatenation or the format proc instead, sorry for the inconvenience.

@krux02 krux02 closed this as completed Oct 28, 2018
@bluenote10
Copy link
Contributor

When closing as a duplicate it would be good to link to it: #7632

@timotheecour timotheecour changed the title String interpolation raises undeclared identifier but identifier is in scope String interpolation (eg fmt) raises undeclared identifier but identifier is in scope Oct 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants