www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Auto and empty array

reply "Igor" <idemura yandex.ru> writes:
In case I want to create a new array with size I know, it's very 
convenient to use auto:
auto a = new int[](13);
But if I want to create a new, empty array? Then using auto is 
not convenient and I have:
auto a = new int[](0);
It seems a bit bloated, compared to int[] a = []. I like using 
auto because of uniform syntax and other reasons. Is there any 
concise way to create a new empty array with auto?
Jun 29 2014
next sibling parent "w0rp" <devw0rp gmail.com> writes:
On Monday, 30 June 2014 at 05:39:38 UTC, Igor wrote:
 In case I want to create a new array with size I know, it's 
 very convenient to use auto:
 auto a = new int[](13);
 But if I want to create a new, empty array? Then using auto is 
 not convenient and I have:
 auto a = new int[](0);
 It seems a bit bloated, compared to int[] a = []. I like using 
 auto because of uniform syntax and other reasons. Is there any 
 concise way to create a new empty array with auto?
Just type int[] a; null and empty slices are indistinguishable in terms of accessing .length and using foreach.
Jun 29 2014
prev sibling next sibling parent "Daniel Murphy" <yebbliesnospam gmail.com> writes:
"Igor"  wrote in message news:dladvaltigiapekureja forum.dlang.org... 

 It seems a bit bloated, compared to int[] a = []. I like using 
 auto because of uniform syntax and other reasons. Is there any 
 concise way to create a new empty array with auto?
auto a = (int[]).init;
Jun 30 2014
prev sibling next sibling parent Daniel Kozak via Digitalmars-d <digitalmars-d puremagic.com> writes:
V Mon, 30 Jun 2014 05:39:36 +0000
Igor via Digitalmars-d <digitalmars-d puremagic.com> napsáno:

 In case I want to create a new array with size I know, it's very 
 convenient to use auto:
 auto a = new int[](13);
 But if I want to create a new, empty array? Then using auto is 
 not convenient and I have:
 auto a = new int[](0);
 It seems a bit bloated, compared to int[] a = []. I like using 
 auto because of uniform syntax and other reasons. Is there any 
 concise way to create a new empty array with auto?
import std.stdio; void main(string[] args) { auto a = new int[13]; auto b = new int[0]; writeln(a.length); writeln(b.length); stdin.readln; }
Jun 30 2014
prev sibling parent Daniel Kozak via Digitalmars-d <digitalmars-d puremagic.com> writes:
V Mon, 30 Jun 2014 05:39:36 +0000
Igor via Digitalmars-d <digitalmars-d puremagic.com> napsáno:

 In case I want to create a new array with size I know, it's very 
 convenient to use auto:
 auto a = new int[](13);
 But if I want to create a new, empty array? Then using auto is 
 not convenient and I have:
 auto a = new int[](0);
 It seems a bit bloated, compared to int[] a = []. I like using 
 auto because of uniform syntax and other reasons. Is there any 
 concise way to create a new empty array with auto?
You can write some helper function: import std.stdio; auto newa(T)(size_t length = 0) { return new T[length]; } void main(string[] args) { auto a = newa!(int)(13); auto b = newa!(int); writeln(typeid(a),":", a.length); writeln(typeid(b),":", b.length); stdin.readln; }
Jun 30 2014