Tuples and compile-time assigned properties

Artur Skawina art.08.09 at gmail.com
Thu Sep 19 09:11:58 PDT 2013


On 09/19/13 17:22, Joseph Rushton Wakeling wrote:
> On 19/09/13 16:55, Artur Skawina wrote:
>> I'm not sure i understand your problem, but you could use structs. ie:
>>
>>     struct VertexProperties {
>>        size_t color;
>>        string name;
>>     }
>>
>> You can get the field names and types at CT and work with that.
> 
> Yes, that's a good suggestion.  I guess I was thinking that it'd be easier to do something like
> 
>     auto g = Graph!(Tuple!(double, "weight"), Tuple!(size_t, "colour", string, "name"));
> 
> ... than to force the user to define a struct up front and pass it as template parameter.

Yeah, support for anonymous structs as template parms would be useful;

    auto g = Graph!(struct { double weight; size_t color; string name });

But that's not currently possible.
 

You can do something like:

    auto g = Graph!q{ double weight; size_t color; string name; };

then, inside the Graph template:

   // template Graph(string F, ...) {
      struct S { mixin (F); }
      // ...look at fields of 'S'...


(This will work if you only need builtin types and/or custom ones already
available in the Graph template)

artur


More information about the Digitalmars-d-learn mailing list