www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - array concatenation

reply Mike <Mike_member pathlink.com> writes:
Hi

What is the correct way to initialize a dynamic array with a single element?
This doesnt seem satisfactory to me:

char[] array;
char c;
...
array = null;
array ~= c;

Is there a better way of doing this without using casts?
Jul 11 2005
next sibling parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"Mike" <Mike_member pathlink.com> wrote in message 
news:datv7a$1dfh$1 digitaldaemon.com...
 Hi

 What is the correct way to initialize a dynamic array with a single 
 element?
 This doesnt seem satisfactory to me:

 char[] array;
 char c;
 ...
 array = null;
 array ~= c;

 Is there a better way of doing this without using casts?
Why do you need the 'array=null' line? It already starts out empty. There have been requests for an api to construct an array using an expression like array!(char)(c) or something but that will most likely get attention after 1.0. For now one can roll such a feature by hand - I recall a post after Walter added typesafe varargs implementing such a template so searching either the announce or main newsgroup will probably turn up something. ps - the digitalmars.d.learn newsgroup might be better suited for this thread.
Jul 11 2005
prev sibling parent terry <terry_member pathlink.com> writes:
In article <datv7a$1dfh$1 digitaldaemon.com>, Mike says...
Hi

What is the correct way to initialize a dynamic array with a single element?
This doesnt seem satisfactory to me:

char[] array;
char c;
...
array = null;
array ~= c;

Is there a better way of doing this without using casts?
Do you mean like this? char [] array = "c"; array.length = 5; array[1..5] = "defg"; printf("%.*s\n",array);
Jul 11 2005