array of elements of various sybtypes

spir denis.spir at gmail.com
Thu Jan 27 05:41:44 PST 2011


On 01/27/2011 05:40 AM, Andrej Mitrovic wrote:
> On 1/27/11, Steven Schveighoffer<schveiguy at yahoo.com>  wrote:
>>   I'm not sure why this works and the other doesn't, but we
>> definitely need something that allows one to control the array type of a
>> literal.
>
> pragma helps in discovering what DMD does sometime. This will error
> out but it will give some useful info:
>
> pragma(msg, typeid( [1,2,cast(ubyte)3] ));
> error: [1,2,cast(int)cast(ubyte)3] ,&D11TypeInfo_Ai6__initZ
>
> So it forces a cast back to int again.
>
> But we can use a postfix to set an unsigned type for the whole array:
>
> writeln(typeid( [1,2,3u] ));  // uint[]
>
> And we can select a string type with a postfix, but we can't use a cast:
>
> void main()
> {
>      writeln(typeid( ["a"d, "b", "c"] ));    // works
>      writeln(typeid( [cast(dchar)"a", "b", "c"] ));  // doesn't work,
> // Error: incompatible types for ((cast(dchar)"a") ? ("b")): 'dchar'
> and 'string'
> }

The latter fails because because you did an error. Indeed D cannot cast a 
/string/ of immutable chars to a char type.
     writeln(typeid( [cast(immutable(dchar)[])"a", "b", "c"] ));
     // immutable(dchar)[][]

But your remark is valid: 'd' is a typing hint that belonds to the literal 
notation itself; not something that applies afterward on an already created 
thing. We need something similar to /initially/ type arrays literals. By 
analogy, the only thing I can imagine is postfixing the (element type) to the 
literal:
      auto ts = [t1, t2]T0;
But it's not really beautiful ;-)

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list