// Code to test array type promotion. import std.stdio; class Fruit { enum color { Red, Orange, Fuchsia }; static char[] [color] colorlist; color mycolor; char [] name; static this() // Love these :) { colorlist[color.Red] = "Red"; colorlist[color.Orange] = "Orange"; colorlist[color.Fuchsia] = "Fuchsia"; } this() { mycolor = color.Fuchsia; name = "Generic Fruit"; } void whatkind() { writefln("%s -- %s", name, colorlist[mycolor]); } } class Apple : Fruit { this() { mycolor = color.Red; name = "A P P L E"; } void core() { writefln(" Coring an apple."); } } class Orange : Fruit { // The extra size of Orange vs. Apple isn't a problem in arrays. struct orangesarebiggerthanapples { int numberofbumps = 42; double ph = 5.3; } this() { mycolor = color.Orange; name = "Orange"; } void squash() { writefln(" SQUASHING AN ORANGE!"); } } void addfavoritefruit(Fruit [] basket, int index) { basket[index] = new Orange; } void main(char [] [] args) { Apple [] lotsofapples; Fruit [] justsomefruits; lotsofapples.length = 5; lotsofapples[0] = new Apple; lotsofapples[1] = new Apple; lotsofapples[2] = new Apple; justsomefruits = lotsofapples; // Dangerous! justsomefruits.addfavoritefruit(3); lotsofapples[4] = new Apple; writefln("Coring some apples:"); foreach (fruit; lotsofapples) { fruit.whatkind(); fruit.core(); // Undefined behavior. // It could call Orange.squash(), // crash, or even call // a function that didn't belong to that // sneaky Orange. } }