digitalmars.D.learn - Creating new types from tuples.
- Evan Davis (25/25) Jun 06 2014 Hello,
- Chris Nicholson-Sauls (3/28) Jun 06 2014 Is there any reason you couldn't (or would rather not) use
- Evan Davis (6/8) Jun 06 2014 That would work. What would be the best way to auto-generate the
- Philippe Sigaud via Digitalmars-d-learn (21/27) Jun 06 2014 Maybe you could use a struct template, with UDP as a template
Hello, I'm looking to use the Tuple type as a way of generating types to represent data in a send recieve connection pair. I created a template to try this: template s_to_c(UDP packetType) { static if (packetType == UDP.ping) { alias Tuple!() s_to_c; } else static if (packetType == UDP.connect) { alias Tuple!(byte) s_to_c; } else static if (packetType == UDP.keep_alive) { alias Tuple!() s_to_c; } } (UDP is a enum with packet types.) This works, but it also means that s_to_c(UDP.ping) is the same type as s_to_c(UDP.keep_alive). I want to be forced to distinguish between types, even if they contain the same fields, so that recieve( (s_to_c!(UDP.ping) value) { }, (s_to_c!(UDP.keep_alive) value) { } ) isn't an error. Any suggestions are welcome, and thanks for any help, -Evan Davis
Jun 06 2014
On Friday, 6 June 2014 at 23:44:04 UTC, Evan Davis wrote:Hello, I'm looking to use the Tuple type as a way of generating types to represent data in a send recieve connection pair. I created a template to try this: template s_to_c(UDP packetType) { static if (packetType == UDP.ping) { alias Tuple!() s_to_c; } else static if (packetType == UDP.connect) { alias Tuple!(byte) s_to_c; } else static if (packetType == UDP.keep_alive) { alias Tuple!() s_to_c; } } (UDP is a enum with packet types.) This works, but it also means that s_to_c(UDP.ping) is the same type as s_to_c(UDP.keep_alive). I want to be forced to distinguish between types, even if they contain the same fields, so that recieve( (s_to_c!(UDP.ping) value) { }, (s_to_c!(UDP.keep_alive) value) { } ) isn't an error. Any suggestions are welcome, and thanks for any help, -Evan DavisIs there any reason you couldn't (or would rather not) use structs rather than tuples?
Jun 06 2014
On Saturday, 7 June 2014 at 03:14:52 UTC, Chris Nicholson-Sauls wrote: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
Jun 06 2014
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;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
Jun 06 2014