Nim achieves these (and many other) goals spectacularly, and falls short in very few places. I'd like to address one (in my view) minor shortcoming.
My proposal is that, in addition to the var statement, we allow a static var statement, similar to the concept used in C.
proc id(): int =
static var nextId = 0
inc(nextId); nextId
could be transformed to
var nextId_X = 0
...
proc id(): int =
inc(nextId_X); nextId_X
where X is chosen so as to avoid name conflicts. I suppose the compiler could:
Of course, the syntax could be modified from that described above.
The Nim compiler (for instance) uses many global vars. I haven't checked them all, but my random sample suggests quite a few are used in only one proc. I'm sure lots of other Nim code (including my own) is similar. I contend that restriction of variable scope, and declarations made close to call-sites are unequivocably a good thing!
I've searched in the obvious places, but I can't find any reference to a discussion about such a feature for Nim.
What do others think? Is this a feature that could benefit Nim? If someone (myself? another volunteer?) were to implement it, is it likely to be accepted into the language?
Thanks
I obviously overlooked it, despite reading the manual closely many times. It seems strange that it's classified as a pragma, as it causes fundamental changes to semantics.
Thanks very much. I'll have a play with it.
Well.... either one calls the pragma static nonetheless or leave it as "global" and extend the documentation with a phrase like "this is what static does in C and other languages". maybe even add "static variables" to the doc and point it to the global pragma then.
EDIT: Can we rename {.compiletime.} to {.static.} then? :-P
I was just asking for what's the conclusion on how to define static shared global variable, I guess it's:
proc isHexNumber(s: string): bool =
var pattern {.global.} = re"[0-9a-fA-F]+"
result = s.match(pattern)
(ie in D, __gshared pattern = ...)
glutDisplayFunc(display)
glutDisplayFunc(display)
found this specially useful in opengl draw hook proc and other cdecl where using parameters is not supported.
glutDisplayFunc(display)