// Code to test array type promotion. import std.stdio; class Fruit { char [] name; this() { name = "Generic Fruit"; } void whatkind() { writefln("%s", name); } } class Apple : Fruit { this() { name = "A P P L E"; } } class Orange : Fruit { this() { name = "Orange"; } } void addfavoritefruit(in Fruit [] basket) { // These two lines produce totally different copying behavior: // basket ~= new Orange; basket[1] = new Orange; } void main(char [] [] args) { Apple [] lotsofapples; Fruit [] justsomefruits; lotsofapples ~= new Apple; lotsofapples ~= new Apple; lotsofapples ~= new Apple; justsomefruits = lotsofapples; // Dangerous! justsomefruits.addfavoritefruit(); // This line shows that even with basket[1] = ...; there is copying behavior // unfortunately it seems that this copy occurs *after* the assignment. // During the return process, perhaps.. lotsofapples ~= new Apple; writefln("justsomefruits:"); foreach (fruit; justsomefruits) { fruit.whatkind(); } writefln("\nlotsofapples:"); foreach (fruit; lotsofapples) { fruit.whatkind(); } }