Covert a complex C header to D
Nicholas Wilson via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Apr 3 04:18:21 PDT 2017
On Monday, 3 April 2017 at 10:04:53 UTC, biocyberman wrote:
> On Monday, 3 April 2017 at 00:00:04 UTC, Nicholas Wilson wrote:
>> On Sunday, 2 April 2017 at 21:43:52 UTC, biocyberman wrote:
>>> template __KHASH_TYPE(string name){
>>> "struct kh_" ~ name ~"_t { " ~
>>> "khint_t n_buckets, size, n_occupied,
>>> upper_bound; " ~
>>> "khint32_t *flags; " ~
>>> "khkey_t *keys; " ~
>>> "khval_t *vals; " ~
>>> "}"
>>>
>>> }
>>
>> Not that you'll get bitten by it in this case but in D the
>> pointer declarator * is left associative.
>>
>> i.e. in C
>>
>> int *pInt, Int; // "Int" is int not an int*
>> int *pInt, Int[3]; // Int is a static array of 3 ints.
>> but in D
>>
>> misleading:
>> int *pInt, Int; // Int is an int*!!
>>
>> wrong:
>> int *pInt, three_Ints[3]; // Error cannot mix declared types
>>
>> not misleading
>> int* pInt, pInt2; // BOTH int*
>>
>> int*pInt; //pointer to int
>> int[3] three_Ints; // static array of 3 ints.
>
> Thank you for some excellent tips, Nicholas Wilson. I made this
> repo https://github.com/biocyberman/klibD. You are more than
> welcome to make direct contributions with PRs there. The next
> milestone want to reach is to complete to conversion of khash.d
> and have to test code with it.
I'm very buy atm but I will give some general tips:
prefer template over string mixins where possible. This will
make the code much more readable.
try to remove Cisms. Seperate declaration and definition is
the most glaring example. But also the function that deal with
the kh_hastable should be member function.
all of the "name" parameters in the macros should not be
needed as D has overloading and mangling to handle that.
Other than that, good luck and learn lots!
More information about the Digitalmars-d-learn
mailing list