Confusing stuff with arrays

Deewiant deewiant.doesnotlike.spam at gmail.com
Thu May 11 10:50:49 PDT 2006


What I was originally trying to do was make an array of type
char[][2][][char[]]. However, I needed to initialise it, which is a major pain
without array literals. A makeArray template essentially did the trick, but I
had to change the array to the type char[][][][char[]], since it seems that
arrays of static arrays don't work as they should, at least in this case.

The code (demonstrating a bunch of different issues, I think) below explains it
best. Am I just confused, or does it warrant a bug report or two?

(Aside from the array literals feature request, obviously <g>)

---

private import std.stdio;

// cheers to http://www.wikiservice.at/d/wiki.cgi?DocComments/Arrays
template makeArray(T){
	T[] makeArray(T[] newArray...){
		return newArray.dup;
	}
}


void main() {
	// this is fine
	char[][2] foo = makeArray!(char[])("foo", "oof");
	writefln(foo);

	// adding a cast to the above makes it not work, strangely enough
	// "e2ir: cannot cast from char[][] to char[][2]"
	//char[][2] quuux = cast(char[][2])makeArray!(char[])("foo", "oof");
	//writefln(quuux);

	// "cannot assign to static array (__arrayArg2)[0]"

	//char[2][] bar = makeArray!(char[2])("bar", "rab");
	//writefln(bar);

	// this is fine
	char[][][] baz = makeArray!(char[][])(makeArray!(char[])("I want", "array
literals"), makeArray!(char[])("arrr", "ghhh"));
	writefln(baz);

	// "cannot assign to static array (__arrayArg7)[0]"
	// "cannot assign to static array (__arrayArg7)[1]"
	//char[][2][] zot = makeArray!(char[][2])(cast(char[][2])makeArray!(char[])("I
want", "array literals"), cast(char[][2])makeArray!(char[])("arrr", "ghhh"));
	//writefln(zot);

	// sort of works, but usage requires cast(char[][][]), defeating the whole point
	char[][2][] qux =
cast(char[][2][])makeArray!(char[][])(makeArray!(char[])("Array", "literals
today"), makeArray!(char[])("pretty", "please?"));
	// at runtime "Error: std.format formatArg"
	// writefln(qux);
	// outputs garbage
	writefln(qux[0]);
	// these work
	writefln(cast(char[][][])qux);
	writefln((cast(char[][][])qux)[0]);

	// an interesting animal...
	// at runtime "Error: array cast misalignment"
	// why did this work in the above case?
	// it seems that having only one array passed to the outer makeArray causes this
	// is the error here, in the above, or just in all this messing about with evil
casting?
	char[][2][] quux = cast(char[][2][])makeArray!(char[][])(makeArray!(char[])("I
want", "array literals"));
	writefln(quux);
}



More information about the Digitalmars-d-learn mailing list