tuples and freeze dried function args

Bill Baxter dnewsgroup at billbaxter.com
Wed Feb 28 15:36:09 PST 2007


torhu wrote:
> Bill Baxter wrote:
>> I think you may be able to get something like what you want using type 
>> tuples and tupleof.
>>
>> alias Tuple!(int,int,int) FArgs;
>> struct S
>> {
>>     FArgs OneSetOfArgs;
>>     FArgs OtherSetOfArgs;
>> }
>> S s;
>> Foo(s.OneSetOfArgs.tupleof);
>>
>> Or something along those lines maybe?
> 
> That doesn't work.  I tried something like it.
> 
> template Tuple(T...)
> {
>     alias T Tuple;
> }
> 
> struct BlockType {
>    Tuple!(int, int, int) color;   // line 34
> }
> 
> blocktypes.d(34): variable blocktypes.BlockType.color is not a 
> per-instance initializable field
> 
> I guess I'll just go with an array for the color.  It was just intended 
> to show off some D features.  But if it's going to get complicated, 
> there's no point to it.  It would just be a silly way of doing something 
> that's really very simple.

Hmm.  Yeh, if making an anonymous color type is all you want to do with 
it I'd definitely recommend just using int[3].  No reason to bring in 
the big guns unless they're really needed.

I like the way Helix does it using anonymous unions:

struct BlockType {
    union {
        int[3] color;
        struct {
           int r;
           int g;
           int b;
        }
    }
}

Then you can use  b.color[2] or b.b whichever suits your needs.

--bb


More information about the Digitalmars-d-learn mailing list