digitalmars.D - macros in importC
- Emmanuel (39/39) Dec 11 I have spent a lot of time finding "ways" to tightly insert C's
- Richard (Rikki) Andrew Cattermole (3/3) Dec 11 I think I need to ask the question, if you have a new language feature
- Paul Backus (12/17) Dec 11 In D, a function call with no arguments can be [written without
I have spent a lot of time finding "ways" to tightly insert C's macro system in importC. ``` #define macro function_calls() ``` maybe a real example in zlib, C programmers are found to write code like ``` #define zlibversion zlibVersion() ``` initially, I was trying to do something like this globally ``` auto zlibversion = zlibVersion(); ``` `auto` because such functions can return anything any type, which we cannot magically know, and then use the variable in our D programs. I tested that and it worked for simple local setups for ctfe possible functions. but then, obviously D tries ctfe on the function being a global variable initializer. but, C programs like that aren't necessarily built for compile time evaluations. the literal text replacements are found to sometimes pass runtime bound functions. like time functions etc. D's strong type system and using global variables for compatibility with that macros looks like an uphill battle. At this point, I have come to the realization that, we need to go manual this time. I have decided to alias the function using the macro as the id. so alias zlibversion = zlibVersion; then we manually lookup the library we are building with D's importC, and then pass the desired arguments. so if you want to use zlibversion in your D program, maybe you have to look up the API and pass it to the alias. in some D program ``` if (zlibversion() == whatever) // do something; ``` Thoughts ???
Dec 11
I think I need to ask the question, if you have a new language feature to represent macros, is there a behavior you would want to be able to express using it?
Dec 11
On Thursday, 11 December 2025 at 10:27:12 UTC, Emmanuel wrote:maybe a real example in zlib, C programmers are found to write code like ``` #define zlibversion zlibVersion() ```In D, a function call with no arguments can be [written without the parentheses][1], so you could translate this to D like this: ```d alias zlibversion = zlibVersion; ``` And usage would looks like this: ```d if (zlibversion == whatever) // do something ``` [1]: https://dlang.org/spec/function.html#optional-parenthesis
Dec 11









"Richard (Rikki) Andrew Cattermole" <richard cattermole.co.nz> 