I'm completely new to nim and I'm trying to reproduce this pascal type so that I can read a response from a socket into it...
TUDPData=packed record
DataType: word;
SubType: word;
Chan: word;
Len: word;
Data: packed record
case byte of
0:(AsChannelStatus01: TChannelStatus01);
1:(AsVoltageList: packed array[0..127] of single);
2:(AsCurrentList: packed array[0..127] of single);
3:(AsAuxValues: packed array[0..47] of single);
4:(AsAuxEU: packed array[0..47] of packed array[0..3] of AnsiChar);
5:(AsChannelSpec: TChannelSpec);
6:(AsSafetySpec: TSafetySpec);
7:(AsStartData01: TStartData01);
8:(AsVersionInfo: TVersionInfo);
9:(AsTestNameAndComments: TTestNameAndComments);
10:(AsSMBInfoStatus: TSMBInfoStatus);
11:(AsSMBScanListEntry: TSMBScanListEntry);
12:(AsSMBGenericWrite: TAsSMBGenericWrite);
13:(AsSMBGenericRead: TAsSMBGenericRead);
140:(AsSMBGenericReadData: TAsSMBGenericReadData);
14:(AsSystemInfo: TSystemInfo);
15:(AsCalDates: TCalDates);
16:(AsChannelStatus: TAsChannelStatus);
17:(AsChannelVARS: TAsChannelVARS);
18:(AsStartDirectMode: TAsStartDirectMode);
19:(AsSetOutputDirectMode: TAsSetOutputDirectMode);
20:(AsSetVAR: TAsSetVAR);
252:(AsSingles: packed array[0..127] of single);
253:(AsDWords: packed array[0..127] of dword);
254:(AsWords: packed array[0..255] of word);
255:(AsBytes: packed array[0..511] of byte);
end);
end;
so I know I can just create a union like so:
type
TUDPData {.packed.} = object
data_type
, sub_data_type
, channel
, len: uint16
data: Data
Data {.packed.} = object {.union.}
version_info: VersionInfo
system_info: SystemInfo
name: TestNameAndComments
channel_status: ChannelStatus
channel_status_matrix: array[128, LimitedChannelStatus]
but I feel like I'm translating this wrong and I should be using object variants probably. I'm fairly green and just trying to learn so just a push in the right direction or some reading material would be super helpful.
Nim's object variants are from Pascal, they are what you need. Smth like:
Data = object {.packed.}
case b: byte
of 0: AsChannelStatus01: TChannelStatus01
of 1: AsVoltageList: array[0..127, single]
# ...
else: discard