Endianness and Tango

Steven Schveighoffer schveiguy at yahoo.com
Thu May 29 10:24:03 PDT 2008


"Mael" wrote
> Okay thanks,
>
> can you just explain your _res trick ? Why does calling the get method 
> with res doesn't work and says "cast(short[])res is not an lvalue" ?
>

_res is an array slice, res is a static array.  Under the hood of the 
compiler, these are viewed as totally separate types.  In fact, two static 
arrays of different size are viewed as different types.  However, one can 
implicitly cast a static array to an array slice, which is what the function 
is expecting.  However, whenever you cast, you are creating a temporary 
variable, which cannot be an lvalue (a value that can be assigned to).  A 
ref parameter must take an lvalue because it must be able to write to it. 
Because Reader uses ref as a parameter for its array, you must pass it a 
true lvalue, which is what _res is.

It would be impossible for Reader to accept ref parameters to all sizes of 
static arrays, since they are all considered different types, and I don't 
even know that you can ref a static array, so you must do this trick.  Very 
kludgy, but it works :)

Hopefully at some point in the future, D will be able to remedy this.

-Steve 




More information about the Digitalmars-d-learn mailing list