casting array literals doesn't work as stated in the docs
bearophile
bearophileHUGS at lycos.com
Sun Jan 3 03:33:19 PST 2010
Trass3r:
> > What's wrong with just (D2 code):
> > short[] a3 = [5, 3];
> Nothing, but it's just the stripped down example.
I was not answering you, but what Denis Koroskin has written. This thread is getting too much confused. And I think I have already seen a very similar thread elsewhere.
> auto t = [0xFF, 0x00];
> yields int[]
That's the correct thing. Ints are the default because D2 doesn't defaults to optimized multi-precision integral values as CLisp.
> Yet this doesn't work:
> http://codepad.org/P9EZ0dIB
module main;
import std.stdio, std.string;
struct RGBA
{
ubyte r;
ubyte g;
ubyte b;
ubyte a;
string toString() { return format("{%s, %s, %s, %s}", r, g, b, a); }
}
static f = cast(RGBA[]) cast(ubyte[])[0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70];
static g = cast(RGBA[]) cast(ubyte[])[0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70].dup;
enum h = cast(ubyte[])[0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70].dup;
static h2 = cast(RGBA[]) h.dup;
enum h3 = cast(RGBA[]) h;
void main()
{
writeln(f);
writeln(h2);
writeln(h3);
}
I don't like that code. So, do you want to define an array of 4-bytes structs?
What's wrong with:
// qualified imports
import std.stdio: writeln;
import std.string: format;
struct RGBA {
ubyte r, g, b, a;
string toString() { return format("{%s, %s, %s, %s}", r, g, b, a); }
}
// alternative 1:
enum RGBA[] data1 = [{0x00, 0x10, 0x20, 0x30}, {0x40, 0x50, 0x60, 0x70}];
// alternative 2:
enum RGBA[] data2 = [RGBA(0x00, 0x10, 0x20, 0x30), RGBA(0x40, 0x50, 0x60, 0x70)];
void main() {
writeln(data1);
writeln(data2);
}
Bye,
bearophile
More information about the Digitalmars-d
mailing list