| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
March 23, 2009 Re: What can you "new" | ||||
|---|---|---|---|---|
| ||||
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?
| ||||
March 24, 2009 Re: What can you "new" | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | 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?
>
>
| |||
March 24, 2009 Re: What can you "new" | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Unknown W. Brackets | Unknown W. Brackets Wrote: > 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." > Best answer yet! You win a free holiday in Tanzania (as long as you pay to get there). Yes it would be great if the error message gave you clue about specifying the size. Then, all the magic is removed, and you know just where you are. But even then, zero would be quite a good default! > -[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? > > > > | |||
March 24, 2009 Re: What can you "new" | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steve Teale | Steve Teale wrote:
> Unknown W. Brackets Wrote:
>
>> 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."
>>
>
> Best answer yet! You win a free holiday in Tanzania (as long as you pay to get there). Yes it would be great if the error message gave you clue about specifying the size. Then, all the magic is removed, and you know just where you are. But even then, zero would be quite a good default!
I don't mean to ruin anyone's holiday in Tanzania but zero is a crappy default. When I say new char[] it's not like I'm really hoping for a shortcut for new char[0]. It's more likely new char[] is really originating as new T where T = char[].
Andrei
| |||
March 24, 2009 Re: What can you "new" | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | No, I agree. I think for the sake of templating, improving the error message is the best option - probably #3 (special case error message) imho.
-[Unknown]
Andrei Alexandrescu wrote:
> Steve Teale wrote:
>> Unknown W. Brackets Wrote:
>>
>>> 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."
>>>
>>
>> Best answer yet! You win a free holiday in Tanzania (as long as you pay to get there). Yes it would be great if the error message gave you clue about specifying the size. Then, all the magic is removed, and you know just where you are. But even then, zero would be quite a good default!
>
> I don't mean to ruin anyone's holiday in Tanzania but zero is a crappy default. When I say new char[] it's not like I'm really hoping for a shortcut for new char[0]. It's more likely new char[] is really originating as new T where T = char[].
>
> Andrei
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply