Either I'm just too stupid, or D is lacking something

Kirk McDonald kirklin.mcdonald at gmail.com
Tue Jul 25 01:32:42 PDT 2006


Don Clugston wrote:
> Derek wrote:
> 
>> On Mon, 24 Jul 2006 22:14:30 +0200, Wolfgang Draxinger wrote:
>>
>>
>>> Probably I'm just too stupid, but if that's really a missing
>>> feature, then I plead to add it.
>>
>>
>> I have trouble understanding mixins too. They seem to solve half the
>> problem I have and create two more while doing so. I suspect that I'm
>> trying to do more than Walter believes should be allowed. The syntax and
>> usage is more obtuse and misleading than C++ templates IMHO.
> 
> 
> They work brilliantly for creating nested functions, but so far I 
> haven't found them to be useful for anything else. I think they'll need 
> to be totally revamped eventually; there's some serious problems with 
> them in their current form. I'd advise against using them if there's a 
> reasonable alternative.

David Rushby's update of the Python header uses them in place of some 
macros in the original C version. Most of the raw Python/C API revolves 
around a series of structs, all of which share a certain series of 
fields in common. So at the root of all these structs is this template:

template PyObject_HEAD() {
     int ob_refcnt;         // The reference count of the object
     PyTypeObject *ob_type; // A pointer to the object's type object
}

Most operations in the API involve passing PyObject*s around; the 
PyObject struct is simply defined like this:

struct PyObject {
     mixin PyObject_HEAD;
}

And then, any other types defined in the API just add their own fields 
after the mixin:

struct PySomeType {
     mixin PyObject_HEAD;
     int some_field;
}

In this way is inheritance simulated in a C API. Where PyObject_HEAD is 
a macro in C, it is a much safer mixin in D.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki



More information about the Digitalmars-d-learn mailing list