More D newb questions.

Ary Borenszweig ary at esperanto.org.ar
Mon May 5 06:47:19 PDT 2008


Me Here wrote:
> Hi all,
> 
> I getting error message from the following code that I do not know how to
> interpret.
> (Please don't critique the code. Its just an exercise :)
> 
> char[4] table[65536];
> 
> void buildTable() {
>     char[] abcd = "abcd";
>     
>     for( ushort b1 = 0; b1 < 4; b1++ ) {
>         for( ushort b2 = 0; b2 < 4; b2++ ) {
>             for( ushort b3 = 0; b3 < 4; b3++ ) {
>                 for( ushort b4 = 0; b4 < 4; b4++ ) {
>                     for( ushort b5 = 0; b5 < 4; b5++ ) {
>                         for( ushort b6 = 0; b6 < 4; b6++ ) {
>                             for( ushort b7 = 0; b7 < 4; b7++ ) {
>                                 for( ushort b8 = 0; b8 < 4; b8++ ) {
>                                     table[ 
>                                         ( b1 << 14 ) | ( b2 << 12 ) | 
>                                         ( b3 << 10 ) | ( b4 <<  8 ) |
>                                         ( b5 <<  6 ) | ( b6 <<  4 ) | 
>                                         ( b7 <<  2 ) | b8
> /* line 88 */                       ] = abcd[ b1 ] ~ abcd[ b2 ] ~ abcd[ b3 ] ~
> abcd[ b4 ]
>                                       ~ abcd[ b5 ] ~ abcd[ b6 ] ~ abcd[ b7 ] ~
> abcd[ b8 ];
>                                    
>                                 }
>                             }
>                         }
>                     }
>                 }
>             }
>         }
>     }
> }
> 
> The errors:
> c:\dmd\test>dmd -O -inline count2Bit.d
> count2Bit.d(88): Error: Can only concatenate arrays, not (int ~ int)
> count2Bit.d(88): Error: Can only concatenate arrays, not (int ~ int)
> count2Bit.d(88): Error: Can only concatenate arrays, not (int ~ int)
> count2Bit.d(88): Error: Can only concatenate arrays, not (int ~ int)
> count2Bit.d(88): Error: Can only concatenate arrays, not (int ~ int)
> count2Bit.d(88): Error: Can only concatenate arrays, not (int ~ int)
> count2Bit.d(88): Error: Can only concatenate arrays, not (int ~ int)
> 
> count2Bit.d(88): Error: cannot assign to static array table[
>     cast(uint)( cast(int)b1 << 14 | cast(int)b2 << 12 | cast(int)b3 << 10 |
> cast(int)b4 << 8 |
>     cast(int)b5 << 6 | cast(int)b6 << 4 | cast(int)b7 << 2 | cast(int)b8)
> ]
> 
> Why does D think I'm trying to concatenate ints, when teh only expressions
> involving catenation are
> slices of char[] abcd?
> 
> Why is D casting my carefully specified ushorts to ints when doing
> bit-twiddling?
> I am specifically trying to avoid signed operations. Even promotion to uints
> would seem
> unnecessary, but its?
> 
> Finally, is it possible to populate a static lookup table at runtime? 
> Or should I just return a ref to the table and assignit to a const pointer in
> the caller stack?
> 
> Cheers, b.

Hi Me Here,

void main() {
	char[4] table;
	
	table = 'a' ~ 'b';
}

main.d(4): Error: Can only concatenate arrays, not (int ~ int)
(and other errors)

'a' and 'b' are chars, and you can only concatenate char[]. It seems the 
compiler is casting 'a' and 'b' to int before concatenating, I don't 
know why. That's why in your example you get the same error.

import std.string;

toString('a') ~ toString('b')

works, but I wonder... why you can't concatenate primitives and let the 
compiler concatenate them by automatically calling toString?



More information about the Digitalmars-d mailing list