The CAPI Manifesto

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Fri Oct 21 01:32:32 PDT 2011


That's ALL you can do in C. fill structs and call functions
(fundamental type manipulation doesn't count).
My personal research shows the following use cases of C macros (sorted
by popularity in descending order):
1. enum
2. alias (most notably, conditionally compiled ones)
3. CTFE function
4. mixin template
5. syntactic alias
6. syntactic mixin template

only the last 2 out of 6 cannot be translated to D.
An example of a syntactic alias is this very common piece of C code:
#ifdef __VERY_VERY_OLD_C_COMPILER__
    #define CONST
#else
    #define CONST const
#endif

An example of a syntactic mixin template is this piece of code, which
i never actually saw anywhere (possible only in C99 and C++):
#define N_TIMES(n) for(int i = 0; i != n; ++i)

The last use case is very rare. The only legitimate example i ever saw
is in libjpeg, where a macro is used to define function pointers of
API functions.
The use case before that is mostly used for portability reasons, which
is not necessary in D.
Some non-standard extension encapsulating macros are almost always
used in C libraries, which can be removed altogether.

The translation can go on regarding the above use cases and the last
two cases can be evaluated in-line, commented out and warned about for
manual translation.

On Fri, Oct 21, 2011 at 11:48 AM, so <so at so.so> wrote:
> On Thu, 20 Oct 2011 00:26:58 +0300, Nick Sabalausky <a at a.a> wrote:
>
>> "so" <so at so.so> wrote in message
>> news:op.v3ivsvb8mpw3zg at localhost.localdomain...
>>>
>>> On Tue, 18 Oct 2011 03:32:17 +0300, Jonathan M Davis
>>> <jmdavisProg at gmx.com>
>>> wrote:
>>>
>>>> Translating C header
>>>> files to D is a pain and time consuming, and it would benefit us all to
>>>> have a
>>>> place to go to find common C headers translated to D so that such work
>>>> doesn't
>>>> have to be duplicated.
>>>
>>> With the second compiler, there won't be any need for us to do anything,
>>> like C++ we could use C libraries seamlessly.
>>
>> The only reason C++ is able to do that is because C++ (realistically, even
>> if not *technically*) is a proper superset of C. D isn't.
>
> Right, but more importantly it is ABI compatible which is what D also has,
> this opens some doors.
> For most C libraries if you exclude macros, you just fill structs, and call
> functions.
>


More information about the Digitalmars-d mailing list