Hi, I was hoping that someone might help me with this.

Is there a smart way to iterate through a custom structured type and the 
determine the size of each of its elements?

In practice, what I need to do is this: I have defined a rather big custom 
structured type which is going to hold data loaded from an external file. 
Depending on the OS endianess, I might have to change the byte order of its 
elements — this being the reason why I need to find a way to determine the byte 
size of each element.

Ideally, I'd like to implement endianess correction with a loop of some kind, 
instead of having to manually fix the bytes of each element of the structured 
type, one after the other, which would result in a rather long code. Also, an 
iteration loop would still work if the structured type changes in the future, 
whereas the manual approach would have to be tweaked in this case.

I'm trying to adapt some C code to Nim. The original C code does actually 
reverse the bytes of each element one by one, but I was hoping Nim could 
provide a smarter approach via some insights into the structured type — after 
all, Nim is famous for providing some amazing ways to do things which look 
impossible in other languages.

It would be perfectly fine to do so at preprocessing or compile time, in case 
metaprogramming or Source Code Filters could help here.

Reply via email to