Hi.
I lost half a day to a very peculiar problem that I can't even figure out after I solved it. I have a minimal shared state object in my project:
# state.nim
import tables
import types/config
import types/config/init
type
State* = ref object
context*: Table[ string, string ]
config*: Config
var state* = State(
config: init_config(),
context: initTable[ string, string ]()
)
This is the current version (with a few local imports, another type declaration and the accompanying init-proc for it) and it works fine. However if I change it to what it used to be:
# state.nim
import tables
import types/config
import types/config/init
type
State* = ref object
context*: Table[ string, string ]
config*: Config
var global_state* = State(
config: init_config(),
context: initTable[ string, string ]()
)
(the only difference is the renaming of the var state to global_state) and now it doesn't work anymore but throws: Error: undeclared identifier: 'context' if I include the file and try to access global_state.context in another file. But the new version works fine...
I did a source search for the name global_state (including variants) in Nim's source, but I can't find anything that points to it being used. I also did:
static:
echo type(global_state)
in state.nim before my variable initialisation to verify that it wasn't already in scope for some reason ...
As I say, the only thing I had to change for this to work was the name ... Any insights? :)
(the only difference is the renaming of the var state to global_state) and now it doesn't work anymore but throws: Error: undeclared identifier: 'context' if I include the file and try to access global_state.context in another file.
One possible reason is that previously you forgot to add the export marker "*" to the field. There really is nothing special about the global_state name.
Could there be an issue where the declared variable got exported to the symbol table as literally "global_state" and all the call sites correctly looked for "globalState"? It's the only thing I could think of that would explain it.
I did have it exported :) But it was one of the first things I checked in my 3/4day bug hunt ...
I'll do some more testing now and see if I can replicate the problem in a minimal project.
@Araq I made a repo for the project, it was on my todo list for this week anyway. You can find it on a branch with the broken code here: https://github.com/accelerate-ssg/acc/tree/naming_bug
If you rename global_state to state it works ... I'd love to know why :)
Disclaimer: First Nim project, code organisation not final, yada yada. Any tips on anything are welcome - if you happen to see egregious violations of idiomatic Nim :D