What can you "new"

Unknown W. Brackets unknown at simplemachines.org
Tue Mar 24 01:31:57 PDT 2009


Steve,

It's not exactly prose, but the error message is correct.  It says:

"Error: new can only create structs, dynamic arrays or class objects, 
not char[]'s."

So:

1. You didn't try to allocate space for a struct (e.g. new struct_t.)
2. You didn't try to allocate space for a dynamic array (new char[5].)
3. You didn't try to allocate space for a class object (new Class.)

 From your code, it's obvious what you were meaning to do, so I would 
agree that changing this would be good.  Options I see are:

1. Improve the error message, e.g.: "Error: new can only create structs, 
sized dynamic arrays, or class objects; char[] cannot be created."

2. Change the compiler to react as if you used new char[0].

3. Special case the error message, e.g.: "Error: new can only create 
dynamic arrays with an initial length, use 0 for empty."

-[Unknown]


Steve Teale wrote:
> bearophile Wrote:
> 
>> Steve Teale:
>>> What am I missing here, isn't char[] a dynamic array?
>> I suggest you to post such questions to the "learn" newsgroup.
>>
>> D dynamic arrays aren't objects, they are C-like structs that contain a just length and a pointer (no capacity). The "new" for them is needed only to allocate the memory they point to. So to define an empty dynamic array of chars:
>>
>> char[] ca;
>> In D1 you can also just:
>> string s1;
>>
>> To allocate a non empty array of chars of specified len:
>> auto ca = new char[some_len];
>>
>> Tale a look at the D docs, where such things are explained.
>>
>> Bye,
>> bearophile
> 
> So how do you interpret the error message?
> 
> 



More information about the Digitalmars-d mailing list