Sane API design (AKA C's #ifdef hell)
Dennis
dkorpel at gmail.com
Fri Apr 17 10:32:37 UTC 2026
On Friday, 17 April 2026 at 08:08:57 UTC, Dejan Lekic wrote:
> It becomes _much more_ complicated when you need to check for
> version, and depending on version declare functions. D does not
> offer good solution for this and that is where C preprocessor
> wins as we have to combine `version`s, enums, static ifs in
> order to achieve things that are quite simple with C
> preprocessor.
>
> Those who believe D is good for this should write a binding for
> a complex library that supports all versions.
I'm looking at https://github.com/nigels-com/glew/tree/master and
https://github.com/BindBC/bindbc-opengl, and I wouldn't say
either is a winner.
The effort needed to accommodate "complex library" and "all
versions" by far eclipses the effort to write a little
boilerplate like:
```D
enum glSupport = (){
version(GL_46) return GLSupport.gl46;
else version(GL_45) return GLSupport.gl45;
else version(GL_44) return GLSupport.gl44;
else version(GL_43) return GLSupport.gl43;
else version(GL_42) return GLSupport.gl42;
else version(GL_41) return GLSupport.gl41;
else version(GL_40) return GLSupport.gl40;
else version(GL_33) return GLSupport.gl33;
else version(GL_32) return GLSupport.gl32;
else version(GL_31) return GLSupport.gl31;
else version(GL_30) return GLSupport.gl30;
else return GLSupport.gl21;
}();
```
https://github.com/BindBC/bindbc-opengl/blob/d3c00b1cb6b11494ed84bb8d9879eb18baa16d6c/source/bindbc/opengl/config.d#L46-L59
I'm not saying D's version system is all flowers and sunshine,
but personally I wish people were more keen on solving the "10000
lines of complex bindings" problem rather than the "100 lines of
boilerplate" problem. ImportC has lots of potential here,
although in practice the abundant use of non-standard C
extensions and macro APIs still necessitates lots of manual work
unfortunately.
More information about the Digitalmars-d
mailing list