Cool thing about D, number #72
Daniel Keep
daniel.keep.lists at gmail.com
Tue Apr 24 07:06:44 PDT 2007
Bill Baxter wrote:
> Daniel Keep wrote:
>>
>> I discovered the same thing, by roughly the same thinking. I was
>> working with DFL and cairo, and was using DFL's color structure.
>> Problem was that DFL's stored using ubytes, and I wanted doubles.
>>
>> So when I was writing my own colour structure, as I was defining the
>> storage, I wrote
>>
>>> union
>>> {
>>> struct { double r, g, b, a; }
>>> double[4] rgba;
>>> }
>>
>> And it suddenly hit me: I could add a tuple!
>
> How's that? You add an alias Tuple!(something ) to the union?
Tuple!(double,double,double,double) tuple;
Actually, I made a template called repeatT that lets me write that as
repeatT!(4, double) tuple, but you get the idea :)
>> Now, I have .tuple members for my colour structs, vectors, quaternions,
>> matrices, and just about every other aggregate type I've written.
>>
>> One thing that would make this an order of magnitude more useful would
>> be the ability to return a tuple from a function.
>>
>> But I agree: it's cool :)
>>
>> -- Daniel
>>
>> P.S. I use .tuple instead of .tupleof because I don't want to shadow
>> the very-useful compile-time property :)
>
> I'm just using the compiler's .tupleof property directly.
>
> --bb
... *slaps forehead*.
In my defense, tho, I imagine it wouldn't have worked quite as well with
the union there :P Also means I can access the elements as individual
variables, as an array *and* as a tuple. The last one comes in handy
for unrolling loops :)
-- Daniel
--
int getRandomNumber()
{
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
http://xkcd.com/
v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
More information about the Digitalmars-d
mailing list