-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
doesn't work properly inside generics
#7632
Comments
I suspect it's related to #5053 as well. |
import strformat
template formatMsg(s: string): string =
&"test {s}"
echo formatMsg("x") |
strformat
doesn't work properly inside generics and templates
I have observed/reported this issue for other string interpolations before (my own outdated and nimboost). In these cases the underlying issue was the behavior of |
We have a plan how to fix it. Templates expecting |
…arargs workaround for stable
Until issue nim-lang#7632 is fixed, use string append.
I spent some time fixing/chaning this part of the language and it's not trivial, some other complex tests fail then... |
* fix nim-lang#9394 by replacing `fmt` with normal string append Until issue nim-lang#7632 is fixed, use string append. * use `strutils.%` instead of normal string add
* fix nim-lang#9394 by replacing `fmt` with normal string append Until issue nim-lang#7632 is fixed, use string append. * use `strutils.%` instead of normal string add
@krux02 In general, could you write a quick comment when you close an issue like that? When I saw that this issue was closed I spend an hour trying to figure out why I still cannot get strformat to work in a template (I was going over the changes in the PR to see how they relate to the discussion here. Only when someone else posted that it is still not working I realized that it isn't a problem on my end). Since it still doesn't work, the issue should either stay open or say "won't fix" to avoid confusion. In any case, I like your workaround, reposting it here for other users: template t(s): untyped =
block:
let x {.inject.} = s
&"test {x}" |
This issue is fixed. The example from this issue has been added to the test case. Your problem is an entirely different problem. By now that example has been added to the documentation of strformat including an explanation why it can't work. It is a structural problem that can't simply be "fixed". In other languages that support string interpolation this problem does not occur because these other languages don't have nim like templates. |
Let's remove the "and template" and track the remaining issue in #10977 ? |
strformat
doesn't work properly inside generics and templatesstrformat
doesn't work properly inside generics
This hasn't really been fixed for generics. The test case in git only works because the main test file |
@jcosborn I just tested it again on development branch of Nim. The example from the issue works. |
Using Nim devel (v0.19.9) with the original test case (with two separate files), it works correctly. Using Nim stable (v0.19.6 or older) it fails with |
Yes, sorry, I was looking at the newer devel tests, but running an older version and 0.19.6. |
This needs 2 files
File 1
File 2
lib/pure/strformat.nim(276, 5) Error: undeclared identifier: 'format'
It seems like within a generic or static proc the compiler tries to resolve the identifier 'format' earlier than expected as
format
is declared after the macros that calls it here on line 489.This seems related to #6387 (Generics proc + macros: identifier resolution happens before macro)
The text was updated successfully, but these errors were encountered: