enum ubyte[] vs enum ubyte[3]

bearophile bearophileHUGS at lycos.com
Mon Dec 20 02:02:54 PST 2010


Johannes Pfau:

Hello Johannes and thank you for developing your tool for D2 too :-)


> Making it output "enum ubyte[30]" would be more complicated, so I wonder  
> if there's a difference between "enum ubyte[]" and "enum ubyte[30]"?

In D1 a enum ubyte[] is a compile-time constant dynamic array of unsigned bytes, it is a 2 word long struct that contains a pointer and a length.
In D1 you express the same thing with "const ubyte[]".

In D2 a "enum ubyte[30]" is a compile-time constant fixed size array of 32 unsigned bytes that gets passed around by value.
In D1 a "const ubyte[30]" is a compile-time constant fixed size array of 32 unsigned bytes that gets passed around by reference.

So they are two different things and you use one or the other according to your needs. Currently there are also some low performance issues in D with enums that get re-created each time you use them (this is true for associative arrays, but I don't remember if this is true for dynamic arrays too). So better to take a look at the produced asm to be sure, if you want to avoid performance pitfalls.

Regardless the array kind you want to use, also take a look at "Hex Strings":
http://www.digitalmars.com/d/2.0/lex.html
That allow you to write bytes arrays as hex data:
x"00 FBCD 32FD 0A"

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list