Forum > Suggestions

Proposal: unions as a new type and arrays with number of elements

<< < (2/24) > >>

flowCRANE:
That's the problem with variant-records, that their functionality is limited and their syntax is tragic.

To answer your question, no, your structure is wrong because Other occupies the same memory space as Chunks and should be after it. The union should be 8 bytes and your structure is 4 bytes. The only solution, given the current syntax and variant-records, is to declare Chunks as a separate type and use it to declare a field. Like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TFooChunks = record  case Byte of    0: (Number: UInt32);                      // TFoo.Chunks.Number    1: (Words: array [0 .. 1] of UInt16);     // TFoo.Chunks.Words    2: (Bytes: record                         // TFoo.Chunks.Bytes    case Byte of      0: (Unsigned: array [0 .. 3] of UInt8); // TFoo.Chunks.Bytes.Unsigned      1: (Signed:   array [0 .. 3] of Int8);  // TFoo.Chunks.Bytes.Signed    end; );  end; type  TFoo = record    Chunks: TFooChunks; // TFoo.Chunks    Other:  UInt32;     // TFoo.Other  end; 
My eyes bleed when I see code like this — I can't even format it properly... That's why Free Pascal should start supporting true unions, and by the way not force you to use this monstrous syntax, with stupid nested case of and whose body is not completed.

The syntax I would like, i.e. the one that allows you to declare unions clearly, consistently and without any restrictions, is in the example below. Pay attention not only to the declaration of the union, but also to the indexation of the arrays (number of elements, not dumb ranges).


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TFoo = union    Chunks: union                     // TFoo.Chunks      Number: UInt32;                 // TFoo.Chunks.Number      Words:  array[2] of UInt16;     // TFoo.Chunks.Words      Bytes:  union                   // TFoo.Chunks.Bytes        Unsigned: array[4] of UInt8;  // TFoo.Chunks.Bytes.Unsigned        Signed:   array[4] of Int8;   // TFoo.Chunks.Bytes.Signed      end;    end;    Other: UInt32;                    // TFoo.Other  end; 
In this particular example, nested unions are to be named because the expected syntax for accessing these fields is as in the comments. Which does not change the fact that there should be support for unnamed fields for those that are records and unions.

korba812:
I was confused that you use "union" for variable records and regular records. So in Pascal it looks like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TFoo = record    Chunks: record                              // TFoo.Chunks      case Byte of      0: (Number: UInt32);                             // TFoo.Chunks.Number      1: (Words:  array [0 .. 1] of UInt16);           // TFoo.Chunks.Words      2: (Bytes:  record                               // TFoo.Chunks.Bytes        case Boolean of          True: (Unsigned: array [0 .. 3] of UInt8);   // TFoo.Chunks.Bytes.Unsigned          False: (Signed:   array [0 .. 3] of Int8);   // TFoo.Chunks.Bytes.Signed        end;);      end;    Other: UInt32;                            // TFoo.Other  end; 

flowCRANE:
You used the additional record to group the common part, to be able to declare something below it. It works, but syntactically sucks, the readability is poor (in my opinion).

korba812:
But exactly the same you show in your example only instead of record you use union. Maybe pascal syntax isn't very pretty, but it's been known for years and isn't confusing. Why another keyword for something that already exists?

flowCRANE:
If you like code stuffed with garbage, well, stick with it. I don't like that, especially when the syntax forces code obfuscation with worthless data such as case of's, their cases and meaningless parentheses that don't match the syntax of structures, when a simple and readable union end is enough. It is the same simple as record end.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version