www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - string "char[]" initialization

reply Tyro <ridimz_at yahoo.dot.com> writes:
The following is minor in the grand scheme of things. However, methinks 
it important enough to warrant attention sometime in D future.

The C++ way:

   int size = 30; char fill = '*';
   std::string cstr(size, fill);

The D way:

   int size = 30; char fill = '*';
   char[] dstr;
   dstr.length = size;
   dstr[] = fill;

Methinks the C++ way is better.

Andrew
Dec 20 2004
next sibling parent reply Vathix <vathix dprogramming.com> writes:
On Mon, 20 Dec 2004 07:02:24 -0500, Tyro <ridimz_at yahoo.dot.com> wrote:

 The following is minor in the grand scheme of things. However, methinks  
 it important enough to warrant attention sometime in D future.

 The C++ way:

    int size = 30; char fill = '*';
    std::string cstr(size, fill);

 The D way:

    int size = 30; char fill = '*';
    char[] dstr;
    dstr.length = size;
    dstr[] = fill;

 Methinks the C++ way is better.

 Andrew

Methinks the D way shows you what's really going on.. You could also use an initializer instead of using length, int size = 30; char fill = '*'; char[] dstr = new char[size]; dstr[] = fill;
Dec 20 2004
next sibling parent reply Tyro <Tyro_member pathlink.com> writes:
In article <opsja3isbqkcck4r tc3-ppp015.dialup.wzrd.com>, Vathix says...
On Mon, 20 Dec 2004 07:02:24 -0500, Tyro <ridimz_at yahoo.dot.com> wrote:

 The following is minor in the grand scheme of things. However, methinks  
 it important enough to warrant attention sometime in D future.

 The C++ way:

    int size = 30; char fill = '*';
    std::string cstr(size, fill);

 The D way:

    int size = 30; char fill = '*';
    char[] dstr;
    dstr.length = size;
    dstr[] = fill;

 Methinks the C++ way is better.

 Andrew

Methinks the D way shows you what's really going on..

You've got a point there Chris. I didn't it too difficult a concept to understand. But yes, I will admit there is ambiguity in the D way.
You could also use an initializer instead of using length,
    int size = 30; char fill = '*';
    char[] dstr = new char[size];
    dstr[] = fill;

Thanks for the pointer! I didn't think about that. Andrew --- [acedwards] at [ieee] dot [org]
Dec 20 2004
parent Tyro <Tyro_member pathlink.com> writes:
In article <cq6nas$8fa$1 digitaldaemon.com>, Tyro says...
In article <opsja3isbqkcck4r tc3-ppp015.dialup.wzrd.com>, Vathix says...


You've got a point there Chris. I didn't it too difficult
a concept to understand. But yes, I will admit there is
ambiguity in the D way.

Ok I'll wait until I wake up before I make anymore posts. What I meant to say was: I didn't think it too difficult a concept to understand. But yes, I will admit that there is no ambiguity in the D way.
Dec 20 2004
prev sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Vathix wrote:
 Methinks the D way shows you what's really going on..
 You could also use an initializer instead of using length,
    int size = 30; char fill = '*';
    char[] dstr = new char[size];
    dstr[] = fill;

Ok, I'm not exactly recommending that you use this, because it's hardly readable, but you can actually do it all in one statement, like this:
 char[] dstr = ((new char[30])[] = '*');

The simple soltuion to ths, IMHO, is to implement it in a library, so that it looks like C++: char[] fill_string(int len, char c) { char[] ret = new char[len]; ret[] = c; return ret; }
Dec 20 2004
parent Tyro <Tyro_member pathlink.com> writes:
In article <cq6pc7$anu$1 digitaldaemon.com>, Russ Lewis says...
Vathix wrote:
 Methinks the D way shows you what's really going on..
 You could also use an initializer instead of using length,
    int size = 30; char fill = '*';
    char[] dstr = new char[size];
    dstr[] = fill;

Ok, I'm not exactly recommending that you use this, because it's hardly readable, but you can actually do it all in one statement, like this:
 char[] dstr = ((new char[30])[] = '*');


Suddenly things don't look all that dark anymore. Thanks!
The simple soltuion to ths, IMHO, is to implement it in a library, so 
that it looks like C++:

char[] fill_string(int len, char c) {
   char[] ret = new char[len];
   ret[] = c;
   return ret;
}

Dec 20 2004
prev sibling next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Tyro wrote:

 The C++ way:
 
   int size = 30; char fill = '*';
   std::string cstr(size, fill);
 
 The D way:
 
   int size = 30; char fill = '*';
   char[] dstr;
   dstr.length = size;
   dstr[] = fill;
 
 Methinks the C++ way is better.

Does the size really matter? :) In Perl it's: $pstr = '*' x 30; --anders
Dec 20 2004
parent Tyro <Tyro_member pathlink.com> writes:
In article <cq6har$23n$1 digitaldaemon.com>,
=?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= says...
Tyro wrote:

 The C++ way:
 
   int size = 30; char fill = '*';
   std::string cstr(size, fill);
 
 The D way:
 
   int size = 30; char fill = '*';
   char[] dstr;
   dstr.length = size;
   dstr[] = fill;
 
 Methinks the C++ way is better.

Does the size really matter? :)

Sorry, size wasn't a good choice of words there. length would have been much better. And no it doesn't matter. Just as long as I can set some length and fill it up with some characters.
In Perl it's: $pstr = '*' x 30;

--anders

Dec 20 2004
prev sibling parent reply David Medlock <amedlock nospam.org> writes:
Tyro wrote:
 The following is minor in the grand scheme of things. However, methinks 
 it important enough to warrant attention sometime in D future.
 
 The C++ way:
 
   int size = 30; char fill = '*';
   std::string cstr(size, fill);
 
 The D way:
 
   int size = 30; char fill = '*';
   char[] dstr;
   dstr.length = size;
   dstr[] = fill;
 
 Methinks the C++ way is better.
 
 Andrew

You are comparing standard D with the STL string class. Once DTL is finished we can compare code. Cheers, Ash
Dec 20 2004
parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"David Medlock" <amedlock nospam.org> wrote in message
news:cq6poj$avn$1 digitaldaemon.com...
 Tyro wrote:
 The following is minor in the grand scheme of things. However, methinks
 it important enough to warrant attention sometime in D future.

 The C++ way:

   int size = 30; char fill = '*';
   std::string cstr(size, fill);

 The D way:

   int size = 30; char fill = '*';
   char[] dstr;
   dstr.length = size;
   dstr[] = fill;

 Methinks the C++ way is better.

 Andrew

You are comparing standard D with the STL string class. Once DTL is finished we can compare code. Cheers, Ash

Since char[] in D is supposed to fill the same roll as std::string in C++ then I'd say the comparison is valid. But to me the D way is so close to the C++ way that it doesn't matter for the rare instances a fill value is actually needed. Then again maybe Tyro has tons of places in his code where he need a fill value, but I can't really imagine why it would be very common. A helper routine to do in 10 keystrokes what the builtins can do in 20 had better be used often in order to justify its existence. -Ben
Dec 20 2004