how to initialize an array of typedef-ed type?

MLT none at anone.com
Fri May 1 06:15:25 PDT 2009


typedef long location ;
location a[] = cast(location[])[1,2,3,4] ;

Seems to work. 
I was afraid of the mess that would happen if I cast (int *) to (long *) in C...
Can I trust it? Is that the "right way" to do it?

MLT Wrote:

> The fact that D has real typedef, not like the fake ones of C++ is, I think, one of its great features.
> 
> But the fact that there is no automatic cast, together with typedef is causing me problems.
> 
> The one I'm currently fighting with is initialization:
> 
> typedef long location ;
> 
> location a[] = [1,2,3,4] ; // doesn't work: Error: cannot implicitly convert expression
> 
> (btw. gdc has no problem, only dmd)
> 
> is there any nice way to do that? Using  [1L,2L,3L,4L] is uglier, and still doesn't work. The only thing that works is
> location a[] = [cast(location)(1), cast(location)(2), cast(location)(3), cast(location)(4)];
> 
> which is SOMEWHAT ugly.
> 
> So, given that typedef is one of the great features of D, are you using it? How? Or are you usually using alias?
> 
> Thanks!



More information about the Digitalmars-d-learn mailing list