Creating new types from tuples.

Philippe Sigaud via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 6 23:12:51 PDT 2014


>> Is there any reason you couldn't (or would rather not) use structs rather
>> than tuples?
>
>
> That would work. What would be the best way to auto-generate the types? I
> have somewhere around 30 already, and the number will grow with this
> project.
>
> Evan Davis

Maybe you could use a struct template, with UDP as a template
parameter. It'll instantiate a different type for each UDP value. If
you add new possible values to the enum, the rest of the code will
follow.

struct SendReceivePair(UDP udp)
{
    /// maybe here some different values, differentiated by static if,
if needed.
}

Then, use these types directly in 'receive':

receive(
  (SendReceivePair!(UDP.ping) value) { },
  (SendReceivePair!(UDP.keep_alive) value) { }
)

If you want to 'retrieve' the UDP template parameter, you can expose
it through an alias:


struct SendReceivePair(UDP udp)
{
    alias packetType = udp;
}

So, given SendReceivePair!(xxx) srp, you can get 'xxx' by srp.packetType;


More information about the Digitalmars-d-learn mailing list