delphi - Why do literals of the form [...] have meaning that appears to depend upon context? -


consider following program:

{$apptype console}  type   tmyenum = (enum1, enum2, enum3);  var   arr: tarray<tmyenum>;   enum: tmyenum;  begin   arr := [enum3, enum1]; // <-- array   enum in arr     writeln(ord(enum));   writeln('---');    enum in [enum3, enum1] // <-- looks array above     writeln(ord(enum));   writeln('---');    readln; end. 

the output is:

 2 0 --- 0 2 --- 

why 2 loops produce different output?

because array contains order information , set not.


explanation use of documentation:

the internal data format of static or dynamic array:

is stored contiguous sequence of elements of component type of array. components lowest indexes stored @ lowest memory addresses.

walking on these indices for in loop is done in incremental order:

the array traversed in increasing order, starting @ lowest array bound , ending @ array size minus one.

on other side, internal data format of set:

is bit array each bit indicates whether element in set or not.

thus these "indiced bits" stored in 1 , same "value". why set can typecasted integer type, , why order in bits added lost: [enum3, enum1] = [enum1, enum3].


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -