August 25, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Benji Smith | Benji Smith a écrit :
> Jarrett Billingsley wrote:
>> Well arrays work _almost_ the same way as in Java, the only difference being that they can change their size.
>>
>> You can either do
>>
>> auto array = new I!(char)[3];
>> array[0] = ..
>> array[1] = ..
>> array[2] = ..
>>
>> Or just declare it and append stuff. The former will be faster since you'll only do 1 memory allocation instead of n (although the implementation does over-allocate for arrays so that appending is faster, you may not want to depend on that for performance-critical code).
>
> Huh. I thought that, by putting an integer literal into the array declaration, it'd be declared as a static array rather than a dynamic array.
If you put an integer into the array declaration, it *is* declared as a static array.
Static array:
-------------
auto array = new I!(char)[3];
- array is of type I!(char)[3] (static array)
- you cannot append using ~
- you can assign or access using indices
- you can initialize it like that or using an array literal
Dynamic array:
--------------
I!(char)[] array;
- array is of type I!(char)[] (dynamic array)
- you can append using ~
- you can assign or access using indices
- you can initialize it using an array literal
|
August 25, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | Ary Borenszweig <ary@esperanto.org.ar> wrote: > If you put an integer into the array declaration, it *is* declared as a static array. > > Static array: > ------------- > auto array = new I!(char)[3]; > - array is of type I!(char)[3] (static array) > - you cannot append using ~ > - you can assign or access using indices > - you can initialize it like that or using an array literal import std.stdio; void main() { auto a = new int[3]; writeln(typeof(a).stringof); } prints "int[]". new expression *always* produces a dynamic array. -- SnakE |
August 25, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | "Ary Borenszweig" <ary@esperanto.org.ar> wrote in message news:g8u542$b39$1@digitalmars.com... > Static array: > ------------- > auto array = new I!(char)[3]; > - array is of type I!(char)[3] (static array) > - you cannot append using ~ > - you can assign or access using indices > - you can initialize it like that or using an array literal No, new I!(char)[3] is syntactic sugar for new I!(char)[](3) and allocates a 3-element I!(char)[]. Fun fact: it is actually impossible within the syntax of D to allocate a static array on the heap. Not really surprising given the horrible treatment of static arrays in general. |
August 26, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sergey Gromov | Sergey Gromov a écrit :
> Ary Borenszweig <ary@esperanto.org.ar> wrote:
>> If you put an integer into the array declaration, it *is* declared as a static array.
>>
>> Static array:
>> -------------
>> auto array = new I!(char)[3];
>> - array is of type I!(char)[3] (static array)
>> - you cannot append using ~
>> - you can assign or access using indices
>> - you can initialize it like that or using an array literal
>
> import std.stdio;
> void main() {
> auto a = new int[3];
> writeln(typeof(a).stringof);
> }
>
> prints "int[]". new expression *always* produces a dynamic array.
Ouch. I first wrote
I!(char)[3] array = new I!(char)[3];
which compiles, and makes array a static array (you can't append to it). Then, for simplicity, I changed the type to auto... it seems new makes a dynamic array, but then it is implicitly converted to a static one, right?
|
August 26, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | "Ary Borenszweig" <ary@esperanto.org.ar> wrote in message news:g8vp2l$11nf$1@digitalmars.com... > > Ouch. I first wrote > > I!(char)[3] array = new I!(char)[3]; > > which compiles, and makes array a static array (you can't append to it). Then, for simplicity, I changed the type to auto... it seems new makes a dynamic array, but then it is implicitly converted to a static one, right? No. When you write auto x = Y; it is identical to writing typeof(Y) x = Y; In this case, typeof(new I!(char)[3]) is I!(char)[]. A dynamic array. |
August 26, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:g911lg$ja5$1@digitalmars.com... > "Ary Borenszweig" <ary@esperanto.org.ar> wrote in message news:g8vp2l$11nf$1@digitalmars.com... >> >> Ouch. I first wrote >> >> I!(char)[3] array = new I!(char)[3]; >> >> which compiles, and makes array a static array (you can't append to it). Then, for simplicity, I changed the type to auto... it seems new makes a dynamic array, but then it is implicitly converted to a static one, right? > > No. When you write > > auto x = Y; > > it is identical to writing > > typeof(Y) x = Y; > > In this case, typeof(new I!(char)[3]) is I!(char)[]. A dynamic array. > Unless you meant in the non-auto case? In which case I'm kind of surprised the compiler allows it since it normally does not allow you to reassign static arrays. |
August 27, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley a écrit :
> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:g911lg$ja5$1@digitalmars.com...
>> "Ary Borenszweig" <ary@esperanto.org.ar> wrote in message news:g8vp2l$11nf$1@digitalmars.com...
>>> Ouch. I first wrote
>>>
>>> I!(char)[3] array = new I!(char)[3];
>>>
>>> which compiles, and makes array a static array (you can't append to it). Then, for simplicity, I changed the type to auto... it seems new makes a dynamic array, but then it is implicitly converted to a static one, right?
>> No. When you write
>>
>> auto x = Y;
>>
>> it is identical to writing
>>
>> typeof(Y) x = Y;
>>
>> In this case, typeof(new I!(char)[3]) is I!(char)[]. A dynamic array.
>>
>
> Unless you meant in the non-auto case? In which case I'm kind of surprised the compiler allows it since it normally does not allow you to reassign static arrays.
Exactly, I meant in the non-auto case. What's going on there?
|
August 28, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ary Borenszweig | "Ary Borenszweig" <ary@esperanto.org.ar> wrote in message news:g92a1n$a9p$1@digitalmars.com... > Jarrett Billingsley a écrit : >> "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:g911lg$ja5$1@digitalmars.com... >>> "Ary Borenszweig" <ary@esperanto.org.ar> wrote in message news:g8vp2l$11nf$1@digitalmars.com... >>>> Ouch. I first wrote >>>> >>>> I!(char)[3] array = new I!(char)[3]; >>>> >>>> which compiles, and makes array a static array (you can't append to it). Then, for simplicity, I changed the type to auto... it seems new makes a dynamic array, but then it is implicitly converted to a static one, right? >>> No. When you write >>> >>> auto x = Y; >>> >>> it is identical to writing >>> >>> typeof(Y) x = Y; >>> >>> In this case, typeof(new I!(char)[3]) is I!(char)[]. A dynamic array. >>> >> >> Unless you meant in the non-auto case? In which case I'm kind of surprised the compiler allows it since it normally does not allow you to reassign static arrays. > > Exactly, I meant in the non-auto case. What's going on there? OK, I just tried it and stepped through and.. what it seems to do is it allocates an int[] of length 3 on the heap, then slice-copies it into the stack-allocated static array. No reference to the array on the heap is kept. Weird. |
September 08, 2008 Re: Templated Interfaces? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | Jarrett Billingsley wrote: > "Benji Smith" <dlanguage@benjismith.net> wrote in message news:g8sih3$7h4$1@digitalmars.com... >> Simen Kjaeraas wrote: >>> You do not need to initialize the the array with the "new I!(char)[]". Just use >>> >>> I!(char)[] array; >>> array ~= new C!(char)('a'); >>> ... >>> char value = array[0].x(); >> Gotcha. >> >> Coming from Java, it's hard getting used to which types require a 'new' and which ones don't. I find the array types especially confusing. >> >> Thanks for your help! >> >> --benji > > Well arrays work _almost_ the same way as in Java, the only difference being that they can change their size. > Also, Java's arrays are Object's, D's arrays are not. -- Bruno Medeiros - Software Developer, MSc. in CS/E graduate http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D |
Copyright © 1999-2021 by the D Language Foundation