help cast

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Mar 18 18:24:31 UTC 2018


On Sunday, March 18, 2018 13:10:28 Steven Schveighoffer via Digitalmars-d 
wrote:
> On 3/18/18 4:34 AM, sdvcn wrote:
> > dchar v11=dchar.max;
> >
> >      auto vp11 = [v11];
> >
> >      auto v2 = cast(ubyte[]) (vp11);   //v2.length=4
> >      auto v22 = cast(ubyte[])( [v11]); //v2.length=1
>
> This seems like a bug to me.
>
> It appears that v22 has truncated v11 to a byte and made only a single
> byte array out of it.

Except that that's precisely how you usually get an array any integral type
smaller than an integer. e.g.

auto arr = cast(ubyte[])([1, 2, 3, 4]);

In this case, you could do

ubyte[] arr = [1, 2, 3, 4];

instead, but if you're not dealing with an initializaton or assignment like
this (e.g. you're passing the array to a functon), then the cast is the way
you do it. Normally, you do it with integer literals, and I could see an
argument that it shouldn't allow it without VRP being used to make it work,
but it _is_ a cast, and casts are a bit of a blunt instrument.

So, I really don't think that it's a bug.

- Jonathan M Davis



More information about the Digitalmars-d mailing list