www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Literal arrays and implicit conversion

Why does this compile?

    void main()
    {
        writefln(typeid(typeof([1,2,3])));
        // prints int[3]
	
        int[] m = [1,2,3];
        const(int)[] c = [1,2,3];
        invariant(int)[] i = [1,2,3];
    }

The way I see it. at least one of these lines should fail to compile.

If the type of [1,2,3] is int[3], then you would expect the assignment
of i to cause a compile error, because you cannot implicitly cast from
int[] to invariant(int)[].

If the type of [1,2,3] is const(int)[3], then you would expect the
assignment of both m and i to cause a compile error, because you
cannot implicitly cast from const(int)[] to either int[] or
invariant(int)[].

If the type of [1,2,3] is invariant(int)[3], then you would expect the
assignment of m to cause a compile error, because you cannot
implicitly cast from invariant(int)[] to int[].

(If the writefln is anything to go by, the first case appears to be in effect).

And yet, all three compile without complaint. Why?
Dec 10 2007