Skip to content

Commit 0e6a0b3

Browse files
authored
fix Issue 24310 - importC: Document workaround for using C symbols which are also D keywords (#3752)
1 parent dc4f26f commit 0e6a0b3

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

spec/importc.dd

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ $(H2 $(LNAME2 examples, Quick Examples))
2626
{
2727
printf("hello world\n");
2828
return 0;
29-
}
30-
)
29+
})
3130

3231
$(P Compile and run:)
3332

3433
$(CONSOLE
3534
dmd hello.c
3635
./hello
37-
hello world
38-
)
36+
hello world)
3937

4038
$(P C function in file $(TT functions.c):)
4139

@@ -391,6 +389,41 @@ $(H2 $(LNAME2 limitations, Limitations))
391389

392390
$(P $(I Compatible Types) (C11 6.7.2) are identical types in ImportC.)
393391

392+
393+
$(H2 $(LNAME2 impedance-mismatch, Impedance Mismatch))
394+
395+
$(P While every effort is made to match up C and D so it "just works", the languages have some
396+
fundamental differences that appear now and then.
397+
)
398+
399+
$(H3 $(LNAME2 keyword-mismatch, Keyword Mismatch))
400+
401+
$(P D and C use mostly the same keywords, C has keywords that D doesn't have, and vice versa. This
402+
does not affect compilation of C code, but it can cause difficulty when accessing C variables and types
403+
from D. For example, the D `version` keyword is not uncommonly used as a struct member in C:)
404+
405+
$(P C code in file $(TT defs.c):)
406+
$(CCODE
407+
struct S { int version; };)
408+
409+
$(P Accessing it from D:)
410+
---
411+
import defs;
412+
int tech(S* s) {
413+
return s.version; // fails because version is a D keyword
414+
}
415+
---
416+
417+
$(P A workaround is available:)
418+
---
419+
import defs;
420+
int tech(S* s) {
421+
return __traits(getMember, *s, "version");
422+
}
423+
---
424+
425+
426+
394427
$(H3 $(LNAME2 same_only_different, Same only Different Types))
395428

396429
$(P On some platforms, C `long` and `unsigned long` are the same size as `int` and `unsigned int`, respectively.

0 commit comments

Comments
 (0)