Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 20, 2004 string "char[]" initialization | ||||
---|---|---|---|---|
| ||||
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 |
December 20, 2004 Re: string "char[]" initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyro | 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;
|
December 20, 2004 Re: string "char[]" initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyro | 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
|
December 20, 2004 Re: string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | 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 |
December 20, 2004 Re: string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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] |
December 20, 2004 Re: string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyro | In article <cq6nas$8fa$1@digitaldaemon.com>, Tyro says... > >In article <opsja3isbqkcck4r@tc3-ppp015.dialup.wzrd.com>, Vathix says... >> [snip] > >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. |
December 20, 2004 Re: string "char[]" initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | 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; } |
December 20, 2004 Re: string "char[]" initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyro | 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
This is not a valid comparison, imo.
You are comparing standard D with the STL string class.
Once DTL is finished we can compare code.
Cheers,
Ash
|
December 20, 2004 Re: string "char[]" initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Medlock | "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 > This is not a valid comparison, imo. > > 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 |
December 20, 2004 Re: string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | 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; >} > |
Copyright © 1999-2021 by the D Language Foundation