-
-
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
Assignment to default
leads to assignment for nodecl
#14253
Comments
Another workaround that has a little bit of readability is {.emit:"const int TEST = 42;".}
let TEST {.importc, nodecl.}: cint = static(default(cint))
echo TEST |
Oh, that is definitely a good workaround. I still think that |
this is very related to nim-lang/RFCs#205 proposal 5. I'd prefer this: # without the preprocessor trick from proposal 5 or in cases where it wouldn't work
let DARWIN_MAXPATHLEN {.importc: "__DARWIN_MAXPATHLEN", header: "<dirent.h>".}: int = discard
# with the preprocessor trick from proposal 5
const DARWIN_MAXPATHLEN {.importc: "__DARWIN_MAXPATHLEN", header: "<dirent.h>".}: int = discard
|
Hmm, I agree that |
I'm fine with other keywords/identifiers than const DARWIN_MAXPATHLEN {.importc: "__DARWIN_MAXPATHLEN", header: "<dirent.h>".}: int = auto note 1:the alternative that was suggested was to skip since (1,3,5):
let foo {.importc.}: int # ok, that can work
const foo {.importc.}: int # parser error for nim < 1.3.5 so it would imply having to backport the parser change to all supported releases (1.0.X and 1.2.X) or forgo of the possiblity of using const (which would be the worst option; const should be used when possible) Hence I stand by my recommendation for since (1,3,5):
let foo {.importc.}: int = auto
const foo {.importc.}: int = auto note 2:this syntax would work too without parser change nor preventing the use of const: since (1,3,5):
let foo {.importc.} = lookup(int)
const foo {.importc.} = lookup(int) (or some other identifier instead of lookup) |
This allows a let statement with the `{.importc.}` pragma to not be initialised with a value. This allows us to declare C constants as Nim lets without putting the value in the Nim code (which can lead to errors, and requires us to go looking for the value). Fixes nim-lang#14253
* Allow let to not have value when using importc This allows a let statement with the `{.importc.}` pragma to not be initialised with a value. This allows us to declare C constants as Nim lets without putting the value in the Nim code (which can lead to errors, and requires us to go looking for the value). Fixes #14253 * Proper fix and documentation + changelog entry * Improve testcase with one from timotheecour * Add test to verify it working with macros
* Allow let to not have value when using importc This allows a let statement with the `{.importc.}` pragma to not be initialised with a value. This allows us to declare C constants as Nim lets without putting the value in the Nim code (which can lead to errors, and requires us to go looking for the value). Fixes nim-lang#14253 * Proper fix and documentation + changelog entry * Improve testcase with one from timotheecour * Add test to verify it working with macros
When assigning with
let
Nim requires a value, even when usingnodecl
. To make it generic/obvious that it's only the default I wanted to usedefault
in order to just get the default value for the type. However this leads to an assignment being generated in the C code.Example
Current Output
Expected Output
Counter example
This works and outputs
42
, so when directly declared to be 0 (or any other value) Nim won't create an assignment. But assignment todefault(cint)
or anything that comes from a proc creates an assignment. The proc case makes sense, and so doesdefault(cint)
, but why doesn't literal values create an assignment? It would be nice if the syntax for this was a bit clearer..Possible Solution
importc, nodecl
would work inlet
without an assignment at all.default(cint)
seems to output the literal 0 to the C code, so it should just do the same as a regular assignment to 0.The text was updated successfully, but these errors were encountered: