pointers-to-members, (custom) array implementation

Wolfgang Draxinger wdraxinger at darkstargames.de
Thu Feb 8 02:57:51 PST 2007


Jarrett Billingsley wrote:

> It's not quite as elegant as pointers-to-members, but it's
> something:
>
> [some hack]

I didn't neglegt the possibility of some hack to get the very
same behaviour. The most ugly variant is possibly the following
(and that works only in some ABI):

class Foo
{
        int bar(char[]);
};

int function(Foo*, char[]) spam;
spam = cast(int function(Foo*, char[])cast(void*)&Foo.bar;

void eggs()
{
        Foo foobar = new Foo;
        spam(&foobar, "spam'n'eggs");
}

In some ABIs this actually works, and it't e.g. the way one uses
DirectX from languages, that don't have interfaces/classes.

But it's ugly and D is all about elegance IMHO. So I think as of
one the next versions D should pointer to members. Those don't
break anything, the language remains orthogonal (since PtM fill
a gap, that one currently has to fill with a hack).







There are also other things _I_ miss and that woudn't really
hurt, to have in D. For example bitfields. Currently the best
way to emulate them is a struct having some static array of
required length and properties to get/set the values. Eventually
a new stucture type "bitfield" may be introduced with some
special rules. E.g. within a bitfield there can be declared no
variables by basic types. One can only specify if a certain
variable is to be treated signed or unsigned and the amount of
bits it consumes. And there should be some way to define
padding. Bitfields should align and the gap to the next
alignment boundary should be consumed, too. And bitfields cannot
stand for themself and must always be embedded into a struct or
union. e.g.

struct bar
{
        bitfield
        {
                unsigned flags:  5;
                signed offset: 4;
                void: 5; // pad 5 bits.
                unsigned count:  15;
        };
        // assume a 4 byte aligning, the bitfield
        // will consume another 3 bits to fill up
};

Since nesting rules apply the members of the annonymous nested
bitfield can be accessed like

bar.flags = 1 | 2 | 8;
bar.offset = -10;
bar.count = 10000;

There are some libraries, e.g. SDL, that use bitfields
extensivly. Having them in the language would be very, very
nice.

I'm not about featureitis, but elegance. As soon as a language
requires me to use some hack, to achieve my goal it doesn't seem
elegant to me.

Wolfgang Draxinger
-- 
E-Mail address works, Jabber: hexarith at jabber.org, ICQ: 134682867




More information about the Digitalmars-d mailing list