digitalmars.D.learn - spurious conversion to int
- Richard Delorme (21/21) Nov 18 2020 The following code
The following code //------------------------ module foo; static immutable ubyte [2] t = [2, 3]; ubyte g(const ubyte a) { return cast (ubyte) (a + 1); } void f(const ubyte a, const ubyte b, const int i) { ubyte [2] c = a > b ? [a.g(), b.g()] : [b, a]; // compile ubyte [2] d = a > b ? [t[i], a] : [a, t[i]]; // compile ubyte [2] e = a > b ? [t[i].g(), a.g()] : [a, t[i]]; // does not compile } //------------------------ does not compile (with DMD 2.94.1). The error message is: foo.d(12): Error: incompatible types for ([g(t[cast(ulong)i]), g(a)]) : ([cast(int)a, cast(int)t[cast(ulong)i]]): ubyte[] and int[] Is this a compiler bug? Or can someone explain me what is wrong with the last line? Why the second array is converted to int[] in this case and not on the above line?
Nov 18 2020