Bug 24312 – importC: Document workaround for using C symbols which are also D keywords
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-01-01T09:01:06Z
Last change time
2024-01-04T15:26:14Z
Keywords
ImportC, pull, spec
Assigned to
No Owner
Creator
Andrej Mitrovic
Comments
Comment #0 by andrej.mitrovich — 2024-01-01T09:01:06Z
I might make a PR for this later, but I'm keeping this as a placeholder.
Initially this was going to be a bug report but I found a neat workaround.
This is similar to https://issues.dlang.org/show_bug.cgi?id=23004, except it's about calling C code on the D side.
clap.c:
-----
typedef struct clap_host {
const char *version;
} clap_host_t;
typedef struct clap_plugin_entry {
void (*init)(const char *plugin_path);
} clap_plugin_entry_t;
-----
And two separate test-cases. They're separate because the error in the first test-case makes the second test-case error disappear (short-circuit of errors I guess):
test1.d:
-----
import clap;
void main() {
clap_host* host;
host.version = null;
}
-----
test2.d:
-----
import clap;
void main() {
clap_plugin_entry* entry;
entry.init(null);
}
-----
```
$ dmd -c test1.d clap.c
test1.d(5): Error: identifier or `new` expected following `.`, not `version`
$ dmd -c test2.d clap.c
test.d(5): Error: function expected before `()`, not `null` of type `clap_plugin_entry*
```
I understand this can't be allowed since it's semantically invalid D code.
There is however a workaround using the getMember trait:
```
import clap;
void main() {
clap_plugin_entry* entry;
__traits(getMember, *entry, "init")(null);
clap_host* host;
__traits(getMember, *host, "version") = null;
}
```
I think it would be good to document this workaround on the ImportC page: https://dlang.org/spec/importc.html
Comment #1 by bugzilla — 2024-01-01T20:24:14Z
good idea
Comment #2 by dlang-bot — 2024-01-01T20:25:12Z
@WalterBright created dlang/dlang.org pull request #3752 "fix Issue 24312 - importC: Document workaround for using C symbols wh…" fixing this issue:
- fix Issue 24312 - importC: Document workaround for using C symbols which are also D keywords
https://github.com/dlang/dlang.org/pull/3752