Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 16, 2001 Int to string | ||||
---|---|---|---|---|
| ||||
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 |
August 17, 2001 Re: Int to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Overlord | 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. |
August 18, 2001 Re: Int to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Overlord | 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 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 > > > |
August 18, 2001 Re: Int to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tim Sweeney | 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 >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 >> >> >> > > |
August 18, 2001 Re: Int to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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 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 > >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; file://same as above > >> > >> > >> > > > > > > |
August 23, 2001 SV: Int to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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 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 > >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; file://same as above > >> > >> > >> > > > > > > |
August 25, 2001 Re: Int to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | > 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
|
February 17, 2002 Re: Int to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | 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.
|
February 18, 2002 Re: Int to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to timeless | "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 '+'. It should have the same precedence as '+'. Any suggestions? > > perl uses '.', i don't know who used it first. D uses ~ already. |
February 20, 2002 Re: Int to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Pavel Minayev | 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. > Good, but there has been some complaints about the tilda not existing 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 |
Copyright © 1999-2021 by the D Language Foundation