A range analysis question

bearophile bearophileHUGS at lycos.com
Wed Jul 10 15:08:12 PDT 2013


Do you know why the assignment to 'item' is accepted in the first 
case and refused in the second?


ubyte generate1(s...)() {
     ubyte[10] result;
     foreach (immutable i, ref item; result)
         item = s[0][0] << 4;
     return result[0];
}

ubyte generate2(s...)() {
     ubyte[10] result;
     foreach (immutable i, ref item; result)
         item = s[0][i % 3] << 4; // line 11
     return result[0];
}

void main() {
     enum ubyte[16] data = [1, 2, 3, 4];
     auto g1 = generate1!data;
     auto g2 = generate2!data;
}


dmd gives:

test.d(11): Error: cannot implicitly convert expression 
(cast(int)[cast(ubyte)1u, cast(ubyte)2u, cast(ubyte)3u, 
cast(ubyte)4u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, 
cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, 
cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, cast(ubyte)0u, 
cast(ubyte)0u][i % 3u] << 4) of type int to ubyte


Bye and thank you,
bearophile


More information about the Digitalmars-d-learn mailing list