www.digitalmars.com         C & C++   DMDScript  

D - Int to string

reply "Overlord" <peewee telia.com> writes:
In D strings can be copied, compared, concatenated, and appended like this.

str1 = str2;
	if (str1 < str3) ...
	func(str3 + str4);
	str4 += str1;

But if you want to add a integer into this, does(or will) D support it like
this(or in some simmilar way)?

int i=10;
char[] str = "abc";


str1 = i;         // str1="10"
if (str == i) ...
func(str + i);   // abc10
str += i; //same as above
Aug 16 2001
next sibling parent "Walter" <walter digitalmars.com> writes:
Overlord wrote in message <9lgrsh$2e8c$1 digitaldaemon.com>...
In D strings can be copied, compared, concatenated, and appended like this.

str1 = str2;
 if (str1 < str3) ...
 func(str3 + str4);
 str4 += str1;

But if you want to add a integer into this, does(or will) D support it like
this(or in some simmilar way)?

int i=10;
char[] str = "abc";


str1 = i;         // str1="10"
if (str == i) ...
func(str + i);   // abc10
str += i; //same as above

Automatic coercion of ints to strings works well in typeless languages like basic and javascript, but such conversions can have unexpected results, so it's probably best to leave that to an explicit conversion.
Aug 17 2001
prev sibling parent reply "Tim Sweeney" <tim epicgames.com> writes:
Using "+" for string concatenation always leads to confusing ambiguities.
In a language without overloading or templates, this can still work, but
requires the user to sometimes perform mental gymnastics figuring out "why
am I getting weird results using + in this context?"

However, if templates or overloading are present, then you run into even
worse ambiguity problems.  To avoid ambiguity, you really need to have
separate syntax for:

    adding (i.e. integers).
    concatenating arrays (strings just like any other case).
    prepending one t to a t[].
    appending one t to a t[].

Note that this is provable rather than speculation. :-)

-Tim

"Overlord" <peewee telia.com> wrote in message
news:9lgrsh$2e8c$1 digitaldaemon.com...
 In D strings can be copied, compared, concatenated, and appended like

 str1 = str2;
 if (str1 < str3) ...
 func(str3 + str4);
 str4 += str1;

 But if you want to add a integer into this, does(or will) D support it

 this(or in some simmilar way)?

 int i=10;
 char[] str = "abc";


 str1 = i;         // str1="10"
 if (str == i) ...
 func(str + i);   // abc10
 str += i; //same as above

Aug 17 2001
parent reply "Walter" <walter digitalmars.com> writes:
I've been thinking that string concatenation should use a different operator
than the poor old '+'. It should have the same precedence as '+'. Any
suggestions?

Some candidates:
    #
    $
    ~
    !
     

Let's see what they look like:

    foo = bar + "hello" + bar + "\n";
    foo = bar # "hello" # bar # "\n";
    foo = bar ~ "hello" ~ bar ~ "\n";
    foo = bar ! "hello" ! bar ! "\n";
    foo = bar   "hello"   bar   "\n";

I kinda like the !. What do other languages use?

Multi-character tokens are also possible, such as !+!, <+>, |+|, -+-, etc.

    foo = bar !+! "hello" !+! bar !+! "\n";
    foo = bar -+- "hello" -+- bar -+- "\n";

Hmm. Doesn't look so good <g>.


Tim Sweeney wrote in message <9lkgci$2og9$1 digitaldaemon.com>...
Using "+" for string concatenation always leads to confusing ambiguities.
In a language without overloading or templates, this can still work, but
requires the user to sometimes perform mental gymnastics figuring out "why
am I getting weird results using + in this context?"

However, if templates or overloading are present, then you run into even
worse ambiguity problems.  To avoid ambiguity, you really need to have
separate syntax for:

    adding (i.e. integers).
    concatenating arrays (strings just like any other case).
    prepending one t to a t[].
    appending one t to a t[].

Note that this is provable rather than speculation. :-)

-Tim

"Overlord" <peewee telia.com> wrote in message
news:9lgrsh$2e8c$1 digitaldaemon.com...
 In D strings can be copied, compared, concatenated, and appended like

 str1 = str2;
 if (str1 < str3) ...
 func(str3 + str4);
 str4 += str1;

 But if you want to add a integer into this, does(or will) D support it

 this(or in some simmilar way)?

 int i=10;
 char[] str = "abc";


 str1 = i;         // str1="10"
 if (str == i) ...
 func(str + i);   // abc10
 str += i; //same as above


Aug 17 2001
next sibling parent "kaffiene" <kaffiene xtra.co.nz> writes:
In practice (Java) + for string concatenation works fine - but there is no
operator overloading in Java, so I guess there's less possibilities for it
to get confused with.

Peter.

"Walter" <walter digitalmars.com> wrote in message
news:9lkk0f$2qdq$1 digitaldaemon.com...
 I've been thinking that string concatenation should use a different

 than the poor old '+'. It should have the same precedence as '+'. Any
 suggestions?

 Some candidates:
     #
     $
     ~
     !
      

 Let's see what they look like:

     foo = bar + "hello" + bar + "\n";
     foo = bar # "hello" # bar # "\n";
     foo = bar ~ "hello" ~ bar ~ "\n";
     foo = bar ! "hello" ! bar ! "\n";
     foo = bar   "hello"   bar   "\n";

 I kinda like the !. What do other languages use?

 Multi-character tokens are also possible, such as !+!, <+>, |+|, -+-, etc.

     foo = bar !+! "hello" !+! bar !+! "\n";
     foo = bar -+- "hello" -+- bar -+- "\n";

 Hmm. Doesn't look so good <g>.


 Tim Sweeney wrote in message <9lkgci$2og9$1 digitaldaemon.com>...
Using "+" for string concatenation always leads to confusing ambiguities.
In a language without overloading or templates, this can still work, but
requires the user to sometimes perform mental gymnastics figuring out


am I getting weird results using + in this context?"

However, if templates or overloading are present, then you run into even
worse ambiguity problems.  To avoid ambiguity, you really need to have
separate syntax for:

    adding (i.e. integers).
    concatenating arrays (strings just like any other case).
    prepending one t to a t[].
    appending one t to a t[].

Note that this is provable rather than speculation. :-)

-Tim

"Overlord" <peewee telia.com> wrote in message
news:9lgrsh$2e8c$1 digitaldaemon.com...
 In D strings can be copied, compared, concatenated, and appended like

 str1 = str2;
 if (str1 < str3) ...
 func(str3 + str4);
 str4 += str1;

 But if you want to add a integer into this, does(or will) D support it

 this(or in some simmilar way)?

 int i=10;
 char[] str = "abc";


 str1 = i;         // str1="10"
 if (str == i) ...
 func(str + i);   // abc10
 str += i; file://same as above



Aug 18 2001
prev sibling next sibling parent "Overlord" <peewee telia.com> writes:
i think the poor old '+' is a goo way to do it because it is simple, not
only can it be used by strings but arrays in general can use it.

But if you really want another operator you can use << and >> like in
cout/cin

str=str1 << str2;   // adds str2 after str1





Walter <walter digitalmars.com> skrev i
diskussionsgruppsmeddelandet:9lkk0f$2qdq$1 digitaldaemon.com...
 I've been thinking that string concatenation should use a different

 than the poor old '+'. It should have the same precedence as '+'. Any
 suggestions?

 Some candidates:
     #
     $
     ~
     !
      

 Let's see what they look like:

     foo = bar + "hello" + bar + "\n";
     foo = bar # "hello" # bar # "\n";
     foo = bar ~ "hello" ~ bar ~ "\n";
     foo = bar ! "hello" ! bar ! "\n";
     foo = bar   "hello"   bar   "\n";

 I kinda like the !. What do other languages use?

 Multi-character tokens are also possible, such as !+!, <+>, |+|, -+-, etc.

     foo = bar !+! "hello" !+! bar !+! "\n";
     foo = bar -+- "hello" -+- bar -+- "\n";

 Hmm. Doesn't look so good <g>.


 Tim Sweeney wrote in message <9lkgci$2og9$1 digitaldaemon.com>...
Using "+" for string concatenation always leads to confusing ambiguities.
In a language without overloading or templates, this can still work, but
requires the user to sometimes perform mental gymnastics figuring out


am I getting weird results using + in this context?"

However, if templates or overloading are present, then you run into even
worse ambiguity problems.  To avoid ambiguity, you really need to have
separate syntax for:

    adding (i.e. integers).
    concatenating arrays (strings just like any other case).
    prepending one t to a t[].
    appending one t to a t[].

Note that this is provable rather than speculation. :-)

-Tim

"Overlord" <peewee telia.com> wrote in message
news:9lgrsh$2e8c$1 digitaldaemon.com...
 In D strings can be copied, compared, concatenated, and appended like

 str1 = str2;
 if (str1 < str3) ...
 func(str3 + str4);
 str4 += str1;

 But if you want to add a integer into this, does(or will) D support it

 this(or in some simmilar way)?

 int i=10;
 char[] str = "abc";


 str1 = i;         // str1="10"
 if (str == i) ...
 func(str + i);   // abc10
 str += i; file://same as above



Aug 23 2001
prev sibling next sibling parent Eric Gerlach <egerlach canada.com> writes:
 I've been thinking that string concatenation should use a different operator
 than the poor old '+'. It should have the same precedence as '+'. Any
 suggestions?
 
 What do other languages use?
 

PHP uses '.'. ex: $hello = "hello"; $world = "world"; print ($hello . " " . $world . "\n"); It's really a nice operator for it. Unfortunately, this creates a parsing nightmare when the language's object reference operator is '.'... I'm a bit partial towards '~' for string concat. Eric
Aug 25 2001
prev sibling parent reply timeless <timeless mac.com> writes:
Walter wrote:
 I've been thinking that string concatenation should use a different operator
 than the poor old '+'. It should have the same precedence as '+'. Any
 suggestions?

perl uses '.', i don't know who used it first.
Feb 17 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"timeless" <timeless mac.com> wrote in message
news:3C702F4D.662B3958 mac.com...

 Walter wrote:
 I've been thinking that string concatenation should use a different


 than the poor old '+'. It should have the same precedence as '+'. Any
 suggestions?

perl uses '.', i don't know who used it first.

D uses ~ already.
Feb 17 2002
next sibling parent reply DrWhat? <DrWhat nospam.madscientist.co.uk> writes:
Pavel Minayev wrote:

 "timeless" <timeless mac.com> wrote in message
 news:3C702F4D.662B3958 mac.com...
 
 Walter wrote:
 I've been thinking that string concatenation should use a
 different operator than the poor old '+'.



Good point - it is always an annoyance explaining that '+' can mean either concatenation or addition in my Java labs, one of the things Java got wrong in my opinion and one of the things beginner can easily get stung by take for example these System.out.println( i+1+": "+args[n] ); // output "<i+1>: <args[n]>" System.out.println( ": "+i+1+args[n]) ); // output ": <i>1<args[n]>"
 It should have the same precedence as '+'. Any suggestions?

perl uses '.', i don't know who used it first.

D uses ~ already.

on some keyboards (at a quick look it is in a different place on each of the FOUR keyboards in front of me currently - and none existant on the fifth, this is not a good situation for touch typists). Not to mention it is difficult to guess (not really a problem - just good practice) and beginners will confuse it with the inversion operator. My suggestions would be symbol ( memory aid ) # ( imagine four + signs ) ( a for add / array ) $ [ well - basic programmers will like it :-) ] ++ ( plus plus end ) |+ ( add to end ) [ ++ may conflict with pre and post increment operators, making parsing difficult - though I presonally would dispose of pre & post operators as they make code difficult to read when overused, I expect that you will want them kept as this coincides with C syntax] C 2001/2/20 "... one of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs." --Robert Firth
Feb 20 2002
parent "Pavel Minayev" <evilone omen.ru> writes:
"DrWhat?" <DrWhat nospam.madscientist.co.uk> wrote in message
news:a50fng$1216$1 digitaldaemon.com...

 symbol  ( memory aid )
 #       ( imagine four + signs )

Actually, it reminds me of math "not equal" sign more...
         ( a for add / array )

Since it usually means "at", I guess it's not the best idea...
 $               [ well - basic programmers will like it :-) ]

UnrealScripters as well. I have nothing against it, although I like tilde a bit better. It reminds me of a "rope": a~b. Sorta mnemonic =)
Feb 20 2002
prev sibling parent "d" <s_nudds hotmail.com> writes:
Yes apparently it was realized that overloading the "+" operator for strings
was confusing.

It's even more confusing for virtually every other overloading function.

That is why operator overloading is a very bad thing and should be avoided.

I laugh.

Pavel Minayev <evilone omen.ru> wrote in message
news:a4q2kp$1i0s$1 digitaldaemon.com...
 "timeless" <timeless mac.com> wrote in message
 news:3C702F4D.662B3958 mac.com...

 Walter wrote:
 I've been thinking that string concatenation should use a different


 than the poor old '+'. It should have the same precedence as '+'. Any
 suggestions?

perl uses '.', i don't know who used it first.

D uses ~ already.

Feb 21 2002