Array literals MUST be immutable.

Steven Schveighoffer schveiguy at yahoo.com
Fri Feb 19 10:26:11 PST 2010


On Fri, 19 Feb 2010 12:59:36 -0500, Michel Fortin  
<michel.fortin at michelf.com> wrote:

> On 2010-02-19 11:03:30 -0500, "Steven Schveighoffer"  
> <schveiguy at yahoo.com> said:
>
>>> //the cast is only there to shut up the stupid dmd!
>>> int[] arr = cast(int[])([0, 1, 2]);
>>  Yes, but a cast is a red flag.  A simple appending of brackets is  
>> not.  It  is well established that casts are to let you do things that  
>> in most  contexts are not a good idea.  Brackets are not the same.
>
> That's the theory. It doesn't quite work in practice because you  
> sometime have to cast to convert from one type to another (float to int,  
> base class to derived), or to make a literal of the type you want  
> (especially with array literals). Many usages of cast are safe, many  
> aren't. So seeing cast as a red flag doesn't really work, unfortunately.
>
> Even SafeD allows cast, it just restricts it to safe usages.

There are some valid uses of casting, but cast is much more of a red flag  
than brackets.  Brackets are used everywhere.  Search your code for [] and  
I'm sure you'll find a lot more instances than casting.

But I think aside from this, dynamic casting should really be a separate  
function, I wouldn't mind if it's functionality moved into the library.

-Steve



More information about the Digitalmars-d mailing list