assemblyscript icon indicating copy to clipboard operation
assemblyscript copied to clipboard

Variable storage for enums

Open MaxGraey opened this issue 3 years ago • 0 comments

Currently enum has a fixed type which is i32. I propose to use a variable type, which will be deduced from the dynamic range of enum items. Like:

export enum Byte { // infer as `u8` because it fit in this range
   Min = 0,
   Mid = 127,
   Max = 255
}

export enum SByte { // infer as `i8`
   Min = -128,
   Mid = 0,
   Max = 127 
}


export enum SByte { // infer as `i16`
   Min = -129,
   Max = 0
}

export enum Flags64 { // infer as `u64`
   Min = 0,
   Max = (1 as i64) << 63
}

For now this:

export enum E {
  A = <i64>2 ** 33
}

will actually compile to:

export enum E {
  A = 0
}

Pros:

  • This will allow you to use enums as bit fields up to 64 values;
  • For arrays from enums with dynamic ranges with the size fit into less than 32-bit will be more economically consumed memory (Array<Byte>, Array<Ubyte>).

Cons:

  • Сasts from/to enum and i32 will be unsafe.

MaxGraey avatar Jul 10 '22 12:07 MaxGraey